本文介绍了如何将一个Drawable资源写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向文件导出一些 Drawable 资源。

I need to export some Drawable resources to a file.

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

For example, I have a function that returns to me a Drawable object. I want to write it out to a file in /sdcard/drawable/newfile.png. How can i do it?

推荐答案

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

Although the best answer here have a nice approach. It's link only. Here's how you can do the steps:

您可以在至少两个不同的方式,这取决于你从哪里获得 Drawable

You can do that in at least two different ways, depending on where you're getting the Drawable from.


  1. Drawable在 res / drawable 文件夹。

  1. Drawable is on res/drawable folders.

假设您要使用可绘制文件夹上的 Drawable 。您可以使用方法。

Say you want to use a Drawable that is on your drawable folders. You can use the BitmapFactory#decodeResource approach. Example below.

Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);




  1. 你有一个对象。

  1. You have a PictureDrawable object.

如果您收到,您可以使用方法来创建您的位图。如下面的例子。

If you're getting a PictureDrawable from somewhere else "at runtime", you can use the Bitmap#createBitmap approach to create your Bitmap. Like the example below.

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)。

Save the Bitmap to disk

Once you have your Bitmap object, you can save it to the permanent storage. You'll just have to choose the file format (JPEG, PNG or 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;
}

要获取目标目录,请尝试如下:

And to get the target directory, try something like:

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,使您的应用程序无响应。

Obs: Remember to do this kind of work on a background Thread if you're dealing with large images, or many images, because it can take some time to finish and might block your UI, making your app unresponsive.

这篇关于如何将一个Drawable资源写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:37