Android - Saving a downloaded image from URL onto SD card

爱⌒轻易说出口 提交于 2019-11-30 07:32:30

You will need to first create the directories and sub-directories where you want to create the files. I see that you used the mkdir() method. Try mkdirs(), and it should work.

Add WRITE_EXTERNAL_STORAGE android permission in your Manifest:

Then

BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

Get to directory (a File object) from SD Card such as:

File sdCardDirectory = Environment.getExternalStorageDirectory();

Create your specific file for image storage:

File image = new File(sdCardDirectory, "download.png");

Then,

boolean success = false;

// Encode the file as a PNG image.
FileOutputStream outStream;
try {

    outStream = new FileOutputStream(image);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    /* 100 to keep full quality of the image */

    outStream.flush();
    outStream.close();
    success = true;
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

if (success) {
    //Display Downloaded
} else {
   // display Error in Downloading
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!