AAPdevelope
AAPdevelope

Reputation: 3

How to save images from resource to public storage on Android?

I want to save an image from the drawable directory to the download folder in the phone.

The code I was trying is:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tradoficial);
    File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    String fileName = "test.jpg";
    File dest = new File(sd, fileName);
    try {
        FileOutputStream out;
        out = new FileOutputStream(dest);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But nothing happens on my phone. Is there anything wrong?

Thanks a lot!

Upvotes: 0

Views: 340

Answers (1)

Blackbelt
Blackbelt

Reputation: 157487

in the AndroidManifest.xml file, add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> under the <manifest> tag

Upvotes: 2

Related Questions