2011-05-05 96 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