创建一个从绘制文件 [英] Create a file from drawable

查看:62
本文介绍了创建一个从绘制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘制folder.All了大量资源,是具有大尺寸大于500KB以上。我有一个srollView加载所有这25个图像的一次。像往常一样,我用尽了内存。请问有什么办法可以减少图像编程的大小。

I have a large number of resources in my drawable folder.All are having big size more than 500KB. I have to load all these 25 images all at once in a srollView. As usual I ran out of memory. Is there any way to reduce the size of the image programmatically.

我得到了这个功能,但它的参数是一个文件,我不知道如何创建从可绘制一个文件。

I got this function but it's parameter is a File and I don't know how to create a File from a drawable.



private Bitmap decodeFile(File f){
    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (FileNotFoundException e) {
    }
    return b;
}

我有一些遍历系统显示内存不足,它是去除从堆栈后面的其他意见后,去thorugh此屏幕循环很多次,但我真的需要它。 请帮我。

I have to go thorugh this screen cyclically many times after a number of traversal the system is showing Low memory and it is removing the other views in the back from the stack , but I actually need it. Please help me.

推荐答案

您可以从您的绘制资源利用打开的InputStream以下code:

You can open an InputStream from your drawable resource using following code:

InputStream is = getResources().openRawResource(id);

在这里 ID 是绘制资源的标识符。对于例如: R.drawable.abc

here id is the identifier of your drawable resource. for eg: R.drawable.abc

现在使用该输入流,你可以创建一个文件。如果您还需要了解如何创建使用该输入流的文件帮助,然后告诉我。

Now using this input stream you can create a file. If you also need help on how create a file using this input stream then tell me.

更新:写在文件中的数据:

try
    {
    File f=new File("your file name");
    InputStream inputStream = getResources().openRawResource(id);
    OutputStream out=new FileOutputStream(f);
    byte buf[]=new byte[1024];
    int len;
    while((len=inputStream.read(buf))>0)
    out.write(buf,0,len);
    out.close();
    inputStream.close();
    }
    catch (IOException e){}
    }

这篇关于创建一个从绘制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆