Andro Selva
Andro Selva

Reputation: 54332

Copy a image from res directory

I have stored some images in res folder of my app. But the user can download this image to their sdcard using the download option. How can I copy the image in res folder to sdcard. Can anyone help me.

Upvotes: 2

Views: 1572

Answers (2)

MKJParekh
MKJParekh

Reputation: 34311

You can do something like this,

    if (isSdPresent()) { // to check is sdcard mounted
            BitmapFactory.Options bmOptions;
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            Bitmap bbicon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

            String extStorageDirectory = Environment.getExternalStorageDirectory()+ File.separator + "FolderName";
            File wallpaperDirectory = new File(extStorageDirectory);
            wallpaperDirectory.mkdirs();
            OutputStream outStream = null;
            File file = new File(wallpaperDirectory,"icon.png");
                     //to get resource name getResources().getResourceEntryName(R.drawable.icon);

        try {
            outStream = new FileOutputStream(file);
            bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();


            } catch (FileNotFoundException e) {
            e.printStackTrace();


            } catch (IOException e) {
            e.printStackTrace();

            }
            } else {

            }

to check SDCard

public boolean isSdPresent() {
        return android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED);
        }

Upvotes: 3

Sephy
Sephy

Reputation: 50442

Maybe this could help you getting a stream from your image and posting it to the user to download...

Upvotes: 1

Related Questions