Save bitmap to location

前端 未结 19 2406
悲哀的现实
悲哀的现实 2020-11-22 00:20

I am working on a function to download an image from a web server, display it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain fo

19条回答
  •  醉话见心
    2020-11-22 01:06

    Some new devices don't save bitmap So I explained a little more..

    make sure you have added below Permission

    
    

    and create a xml file under xml folder name provider_paths.xml

    
    
        
    
    

    and in AndroidManifest under

    
                
            
    

    then simply call saveBitmapFile(passYourBitmapHere)

    public static void saveBitmapFile(Bitmap bitmap) throws IOException {
            File mediaFile = getOutputMediaFile();
            FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, getQualityNumber(bitmap), fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        }
    

    where

    File getOutputMediaFile() {
            File mediaStorageDir = new File(
                    Environment.getExternalStorageDirectory(),
                    "easyTouchPro");
            if (mediaStorageDir.isDirectory()) {
    
                // Create a media file name
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                        .format(Calendar.getInstance().getTime());
                String mCurrentPath = mediaStorageDir.getPath() + File.separator
                                + "IMG_" + timeStamp + ".jpg";
                File mediaFile = new File(mCurrentPath);
                return mediaFile;
            } else { /// error handling for PIE devices..
                mediaStorageDir.delete();
                mediaStorageDir.mkdirs();
                galleryAddPic(mediaStorageDir);
    
                return (getOutputMediaFile());
            }
        }
    

    and other methods

    public static int getQualityNumber(Bitmap bitmap) {
            int size = bitmap.getByteCount();
            int percentage = 0;
    
            if (size > 500000 && size <= 800000) {
                percentage = 15;
            } else if (size > 800000 && size <= 1000000) {
                percentage = 20;
            } else if (size > 1000000 && size <= 1500000) {
                percentage = 25;
            } else if (size > 1500000 && size <= 2500000) {
                percentage = 27;
            } else if (size > 2500000 && size <= 3500000) {
                percentage = 30;
            } else if (size > 3500000 && size <= 4000000) {
                percentage = 40;
            } else if (size > 4000000 && size <= 5000000) {
                percentage = 50;
            } else if (size > 5000000) {
                percentage = 75;
            }
    
            return percentage;
        }
    

    and

    void galleryAddPic(File f) {
            Intent mediaScanIntent = new Intent(
                    "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
    
            this.sendBroadcast(mediaScanIntent);
        }
    

提交回复
热议问题