我怎么能写一个绘制资源到一个文件?文件、资源、我怎么能

由网友(放肆旳青春﹌丢了灵魂)分享简介:我需要一些绘制对象资源导出到文件中。 例如,我有一个函数,返回给我一个绘制对象对象。我想写出来,以一个文件的 /sdcard/drawable/newfile.png 。我怎样才能做到这一点?解决方案 尽管最好的答案在这里有一个很好的方法。它的唯一联系。这里是你如何能做到的步骤:转换绘制对象为位图您可以做,在至少两种...

我需要一些绘制对象资源导出到文件中。

例如,我有一个函数,返回给我一个绘制对象对象。我想写出来,以一个文件的 /sdcard/drawable/newfile.png 。我怎样才能做到这一点?

解决方案

尽管最好的答案在这里有一个很好的方法。它的唯一联系。这里是你如何能做到的步骤:

转换绘制对象为位图

您可以做,在至少两种不同的方式,这取决于你得到的绘制对象从。

绘制对象是 RES /绘制文件夹。

假设你想使用绘制对象这是对你绘制的文件夹。您可以使用 BitmapFactory#德codeResource 方法。下面的例子。

 位图BM = BitmapFactory.de codeResource(mContext.getResources(),R.drawable.your_drawable);
 
首创第一本单机游戏外挂制作教程文档类 C 文档类资源 CSDN下载

您有 PictureDrawable 的对象。

如果你得到一个 PictureDrawable 从别的地方在运行时,您可以使用 位图#createBitmap 的方法来创建你的位图。就像下面的例子。

 公共位图drawableToBitmap(PictureDrawable PD){
    位图BM = Bitmap.createBitmap(pd.getIntrinsicWidth(),pd.getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
    帆布油画=新的Canvas(BM);
    canvas.drawPicture(pd.getPicture());
    返回BM;
}
 

保存位图到磁盘

一旦你有你的位图的对象,你可以将它保存到永久存储。你只需要选择的文件格式(JPEG,PNG或的WebP)。

  / *
* Bitmap.Com pressFormat可以是PNG,JPEG或的WebP。
*
*质量从1到100(百分比)。
*
* DIR你可以从很多地方像Environment.getExternalStorageDirectory()或mContext.getFilesDir得到()
*根据您要保存的图像。
* /

公共布尔saveBitmapToFile(文件目录,字符串文件名,位图BM,
    Bitmap.Com pressFormat格式,诠释品质){

    文件镜像文件=新的文件(目录,文件名);

    FileOutputStream中FOS = NULL;
    尝试 {
        FOS =新的FileOutputStream(镜像文件);

        bm.com preSS(格式,质量,FOS);

        fos.close();

        返回true;
    }
    赶上(IOException异常E){
        Log.e(应用程序,e.getMessage());
        如果(FOS!= NULL){
            尝试 {
                fos.close();
            }赶上(IOException异常E1){
                e1.printStackTrace();
            }
        }
    }
    返回false;
}
 

和获取目标目录,你可以试试:

 文件DIR =新的文件(Environment.getExternalStorageDirectory()+文件分割符+绘制);

布尔doSave的=真;
如果(!dir.exists()){
    doSave的= dir.mkdirs();
}

如果(doSave的){
    saveBitmapToFile(目录,theNameYouWant.png,BM,Bitmap.Com pressFormat.PNG,100);
}
其他 {
    Log.e(应用程序,无法创建目标目录。);
}
 

观测值:记住做这样的工作。如果你正在处理大量的图像,或者许多图像的后台线程,因为它可能需要一些时间才能完成,并可能会阻止你的用户界面,使您的应用程序没有响应。

I need to export some Drawable resources to a file.

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:

Convert Drawable to Bitmap

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

Drawable is on res/drawable folders.

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

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;
}

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

/*
* Bitmap.CompressFormat can be PNG,JPEG or WEBP.
*
* quality goes from 1 to 100. (Percentage).
*
* dir you can get from many places like Environment.getExternalStorageDirectory() or mContext.getFilesDir() 
* depending on where you want to save the image.
*/

public 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: 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.

阅读全文

相关推荐

最新文章