2011-05-05 99 views
11

我的可绘制文件夹中有大量资源。所有文件都大于500KB。我必须在srollView中一次加载所有这25个图像。像往常一样,我耗尽了内存。有没有什么办法来以编程方式减少图像的大小。从可绘制文件创建文件

我得到这个函数,但它的参数是一个文件,我不知道如何从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此屏幕循环多次的一些遍历系统显示内存不足,它是去除从堆栈后面的其他意见后,但我真的需要它。 请帮帮我。

回答

31

您可以从您绘制资源使用下面的代码打开一个InputStream:

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

这里id是你绘制资源的标识符。例如:R.drawable.abc

现在使用这个输入流,您可以创建一个文件。如果您还需要关于如何使用此输入流创建文件的帮助,请告诉我。

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

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){} 
    } 
+0

感谢您的帮助。我得到这个错误... InputStream is =(InputStream)getResources()。openRawResource(R.drawable.image);请告诉我如何创建该文件也 – James 2011-05-05 10:13:14

+1

我不得不像这样施放它。 InputStream是=(InputStream)getResources()。openRawResource(R.drawable.simplelist); – James 2011-05-05 10:13:42

+0

“getResources”是一个在Activity类下定义的函数,因此您确定您拥有活动的上下文,您可以在其中编写获取输入流的代码。 – mudit 2011-05-05 10:14:56

-1

我从@ mudit的答案线索,并创建从输入流中绘制。然后将可绘图加载到适配器中的ImageView中。

InputStream inputStream = mContext.getResources()。openRawResource(R.drawable.your_id);

Bitmap b = BitmapFactory.decodeStream(inputStream); 
b.setDensity(Bitmap.DENSITY_NONE); 
Drawable d = new BitmapDrawable(b); 
mImageView.setImageDrawable(d); 

Here是解决

0

我,所以我更喜欢使用this喜欢的快捷键的详细版本。

用于从drawable生成文件,请参阅this

将这个在您的的build.gradle

compile 'id.zelory:compressor:1.0.4' 

何地,你要压缩的图像把

Bitmap compressedImageFile = Compressor.getDefault(context).compressToBitmap(your_file); 

README.md提供更多的信息。对不起,6年后带来答案。

0

兄弟有两种方法

  1. 一个最简单的使用一些第三方库
  2. 或者使用位图类来绘制下来

规模只是用Bitmap.createScaledBitmap方法来压缩可绘制

步骤:

//步骤1将绘制,并将其转换为位图

Bitmap b = BitmapFactory.decodeResource(context , resId)

//第2步重新调整你的位图

Bitmap nBitmap = b.createScaledBitmap(getResources() , newHieght , newWidth , true); 

//步骤3创建从位图可抽拉

BitmapDrawable drawable = new BitmapDrawable(nBitmap); 

我强烈建议您使用第3部分库,因为此方法非常昂贵

like https://github.com/Tourenathan-G5organisation/SiliCompressor