【问题标题】:How can I write a Drawable resource to a File?如何将 Drawable 资源写入文件?
【发布时间】:2012-04-27 19:10:43
【问题描述】:

我需要将一些Drawable 资源导出到文件中。

例如,我有一个函数返回给我一个Drawable 对象。我想把它写到/sdcard/drawable/newfile.png 的文件中。我该怎么做?

【问题讨论】:

  • 您可以尝试以下答案之一。

标签: android export drawable


【解决方案1】:

虽然这里的最佳答案有一个很好的方法。它只是链接。步骤如下:

将 Drawable 转换为位图

您至少可以通过两种不同的方式做到这一点,具体取决于您从哪里获得 Drawable

  1. 可绘制对象位于 res/drawable 文件夹中。

假设您想使用可绘制文件夹上的Drawable。您可以使用BitmapFactory#decodeResource 方法。下面的例子。

Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);
  1. 您有一个PictureDrawable 对象。

如果您“在运行时”从其他地方获得PictureDrawable,您可以使用Bitmap#createBitmap 方法来创建您的Bitmap。就像下面的例子。

public Bitmap drawableToBitmap(PictureDrawable pd) {
    Bitmap bm = Bitmap.createBitmap(pd.getIntrinsicWidth(), pd.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    canvas.drawPicture(pd.getPicture());
    return bm;
}

将位图保存到磁盘

拥有Bitmap 对象后,您可以将其保存到永久存储中。您只需选择文件格式(JPEG、PNG 或 WEBP)。

/**
 * @param dir you can get from many places like Environment.getExternalStorageDirectory() or mContext.getFilesDir() depending on where you want to save the image.
 * @param fileName The file name.
 * @param bm The Bitmap you want to save.
 * @param format Bitmap.CompressFormat can be PNG,JPEG or WEBP.
 * @param quality quality goes from 1 to 100. (Percentage).
 * @return true if the Bitmap was saved successfully, false otherwise.
 */
boolean saveBitmapToFile(File dir, String fileName, Bitmap bm, 
    Bitmap.CompressFormat format, int quality) {
    
    File imageFile = new File(dir,fileName);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(imageFile);

        bm.compress(format,quality,fos);

        fos.close();

        return true;
    }
    catch (IOException e) {
        Log.e("app",e.getMessage());
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return false;
}

要获取目标目录,请尝试以下操作:

File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "drawable");

boolean doSave = true;
if (!dir.exists()) {
    doSave = dir.mkdirs();
}

if (doSave) {
    saveBitmapToFile(dir,"theNameYouWant.png",bm,Bitmap.CompressFormat.PNG,100);
}
else {
    Log.e("app","Couldn't create target directory.");
}

Obs:如果您正在处理大图像或许多图像,请记住在后台线程上执行此类工作,因为它可能需要一些时间才能完成并且可能会阻塞您的 UI,使您的应用无响应。

【讨论】:

  • 你好。此答案仅在需要图像时才有帮助。但是我怎样才能像文件一样复制矢量可绘制资源,而不需要位图等?
【解决方案2】:

获取存储在 sdcard 中的图像..

File imgFile = new  File(“/sdcard/Images/test_image.jpg”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

更新:

String path = Environment.getExternalStorageDirectory()+ "/Images/test.jpg"; 
File imgFile = new File(path);

【讨论】:

  • SD卡目录不要使用固定路径,应该使用Environment.getExternalStorageDirectory().getPath();
  • 字符串路径 = Environment.getExternalStorageDirectory()+ "/Images/test.jpg";文件 imgFile = 新文件(路径);
  • 好的,但我不想从 sdcard 读取文件,我想在文件中写入可绘制资源...
  • 嗯,它可能是一个正确的答案,即使它不是这个问题的答案。 :-)
猜你喜欢
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 2011-06-16
相关资源
最近更新 更多