2016-06-01 11 views
1

私のアプリの起動時に、後でFileProviderで使用するために、drawableフォルダから内部のappフォルダに自分のイメージ(存在しない場合)を抽出したいと思います。画像サイズは2000 * 2000、平均サイズは380kb、形式はpngです。
これらの画像は表示されません(小さいものは表示に使用されます)。彼らはファイル共有のためだけで、私は元のサイズを維持する必要があります。
私はあなたがそれで行われ、あなたのビットマップオブジェクトのたびにリサイクルを呼び出す必要がファイルを正しく抽出する方法(メモリ不足)?

コード

private void extractImages() { 
    TypedArray imgs = getResources().obtainTypedArray(R.array.smile_list_share); 
    File imagePath = new File(getFilesDir(), "images"); 
    File checkImage; 
    for (int i = 0; i < imgs.length(); i++) { 
     int imageResID = imgs.getResourceId(i, 0); 
     if (imageResID > 0) { 
     String name = getResources().getResourceEntryName(imageResID); 
     checkImage = new File(imagePath, name + ".png"); 

     if (!checkImage.exists()) { 
      Bitmap bm = BitmapFactory.decodeResource(getResources(), imageResID); 
      boolean b = saveBitmapToFile(imagePath, name + ".png", bm, Bitmap.CompressFormat.PNG, 100); 

      Log.e("mcheck","saved "+b+", file "+name); 
      Log.e("mcheck", "file does not exists " + name); 
     } else { 
      Log.e("mcheck", "file exists " + name); 
     } 
     } else { 
     Log.e("mcheck", "ERROR " + i); 
     } 
    } 
    imgs.recycle(); 
    } 

    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); 
     bm.recycle(); 
     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; 
    } 

答えて

0

ビットマップオブジェクトを作成する必要はまったくありません。 getResoursesからintputストリームを直接取得することは可能です。

public boolean saveBitmapToFile(File dir, String fileName, int imageResourse) { 

    File imageFile = new File(dir, fileName); 

    FileOutputStream fos = null; 
    InputStream inputStream = null; 
    try { 
     fos = new FileOutputStream(imageFile); 
     inputStream = getResources().openRawResource(imageResourse); 
     int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 
     int len = 0; 
     while ((len = inputStream.read(buffer)) != -1) { 
     fos.write(buffer, 0, len); 
     } 
     inputStream.close(); 
     fos.close(); 

     return true; 
    } catch (IOException e) { 
     Log.e("app", e.getMessage()); 
     if (fos != null) { 
     try { 
      fos.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     } 
     if (inputStream != null) { 
     try { 
      inputStream.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     } 
    } 
    return false; 
    } 
0

を呼び出すことで、メモリから抜け出します。 ここでは、ビットマップを効率的に操作する方法の良いガイドです。 https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

+0

ビットマップはsavebitmapToFileメソッドでリサイクルされますが、このメソッドが呼び出される前にmemmoryストライクから呼び出されます。また、これらの画像は表示されるべきではありません。ファイル共有に必要です。そのため、元の寸法を維持する必要があります。 – Yarh

+0

oh my badあなたのsaveBitmapToFile()メソッドが表示されませんでした。あなたのクラッシュはどこで起こっていますか?この行?ビットマップbm = BitmapFactory.decodeResource(getResources()、imageResID); – Tristan

+0

はい、それをチェックするために私は他のすべてをコメルして、それの後にbm.recycle straitを入れてmemmoryから出しました – Yarh