Save Bitmap in Android as JPEG in External Storage in a folder

前端 未结 5 1170
日久生厌
日久生厌 2020-12-08 20:33

I am using this code to save Bitmap in External Storage but it does not create the folder if it not exists:

String path = Environment.getExternalStorageDirec         


        
5条回答
  •  忘掉有多难
    2020-12-08 21:02

    late but might be helpful to someone. use below code it will save the bitmap in external directory more faster because of BufferOutPutStream.

    public boolean storeImage(Bitmap imageData, String filename) {
        // get path to external storage (SD card)
    
        File sdIconStorageDir = null;
    
        sdIconStorageDir = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/myAppDir/");
        // create storage directories, if they don't exist
        if (!sdIconStorageDir.exist()) {
            sdIconStorageDir.mkdirs();
        }
        try {
            String filePath = sdIconStorageDir.toString() + File.separator + filename;
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
            //Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();
            // choose another format if PNG doesn't suit you
            imageData.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
    
        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        }
        return true;
    }
    

提交回复
热议问题