Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

You can do something like this,

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

            StringextStorageDirectory= Environment.getExternalStorageDirectory()+ File.separator + "FolderName";
            FilewallpaperDirectory=newFile(extStorageDirectory);
            wallpaperDirectory.mkdirs();
            OutputStreamoutStream=null;
            Filefile=newFile(wallpaperDirectory,"icon.png");
                     //to get resource name getResources().getResourceEntryName(R.drawable.icon);try {
            outStream = newFileOutputStream(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);
        }

Solution 2:

Post a Comment for "Copy A Image From Res Directory"