从res目录复制图像

时间:2011-10-31 12:05:18

标签: android

我已将一些图片存储在我的应用的res文件夹中。但是用户可以使用下载选项将此图像下载到他们的SD卡。如何将res文件夹中的图像复制到SD卡。谁能帮我。

2 个答案:

答案 0 :(得分:3)

你可以这样做,

    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 {

            }

检查SDCard

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

答案 1 :(得分:1)

也许this could help you从您的图片中获取流并将其发布给用户下载...