2016-04-11 9 views
0

プロジェクト:https://github.com/MediaPortal1/HowToDraw/blob/master/app/src/main/java/com/poltavets/app/setImageBitmapエラー、ImageViewのコールのOutOfMemory GitHubの上のエラーと

I持っていた方法の私のプロジェクトでimg.setImageResources(R.id.image)が、私は多くの場合、このメソッドを使用する場合、それはOutOfMemoryを呼び出すofftenまたは変更画面の向き。私はこれを入れて

Handler handler=new Handler(){ 
      @Override 
      public void handleMessage(Message msg) { 
       if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 
        imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,100,100)); 
       else imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,400,400)); 
      } 
     }; 
     handler.sendEmptyMessage(image); 

 imageView.setImageResource(image); 

TO:2つの最後に、この記事の方法、およびからの私のコードを変更https://developer.android.com/training/displaying-bitmaps/load-bitmap.html コピー&ペースト: 私は「右」搭載のビットマップイメージについては、この記事を見つけましたハンドラーへのメソッドとそれはより速く仕事でした。そして、私は方向性の条件を追加します。なぜなら、陸上では、ImageViewは小さく、ビットマップはより小さい解像度でなければならないからです。それはうまくいった、everithinkは大丈夫だった。しかし、私はこのプロジェクトの別の細部(私のプロジェクトに画像を追加するだけ)で作業し、このプロジェクトを2〜3回再構築し、私の活動を開始するとOutOfMemoryエラーが再び現れます。このエラーが起きる前に必ずしも起こらなかったのですが、今私はアクティビティを開始するときにいつも起きました!どうやって?私はこの活動のコードを指摘していませんか?私のイメージは本当に低モーメントです。画像の最大サイズ200kb。 100kbのサイズの画像はどのようにOutOfMemoryを呼び出すことができますか?

エラー:ChangeImageの

Process: com.poltavets.app.howtodraw, PID: 28720 java.lang.OutOfMemoryError: Failed to allocate a 35389452 byte allocation with 5482128 free bytes and 5MB until OOM 
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:655) 
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:488) 
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:511) 
    at com.poltavets.app.howtodraw.view.HowTo.decodeSampledBitmapFromResource(HowTo.java:224) 
    at com.poltavets.app.howtodraw.view.HowTo$3.handleMessage(HowTo.java:185) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5546) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 

コード(エラーはこちら):Googleの記事から

public void changeImageSrc(int image,int count,int position,String name) { 
      filename=name+"_"+image+"_"+position; 
      imagenumber=position; 
      if(move!=null || back!=null) { 
       move.setEnabled(false); 
       back.setEnabled(false); 
      } 
      Handler handler=new Handler(){ 
       @Override 
       public void handleMessage(Message msg) { 
        if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 
         imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,100,100)); 

else imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), msg.what,400,400)); // OUT OF MEMORY ERROR 
       } 
      }; 
      handler.sendEmptyMessage(image); 

      getSupportActionBar().setTitle(getResources().getString(R.string.title_how_to) + " " + name + ": " + (position+1)+"/"+(count)); 
       if(position==0 || position+1==count){ 
        if(position==0){ 
         findViewById(R.id.backBtn).setEnabled(false); 
         findViewById(R.id.backBtn).setVisibility(View.INVISIBLE); 
        } 
        else { 
         findViewById(R.id.moveBtn).setEnabled(false); 
         findViewById(R.id.moveBtn).setVisibility(View.INVISIBLE); 
         Toast.makeText(getApplicationContext(),getString(R.string.finish),Toast.LENGTH_SHORT).show(); 
        } 
       } 
       else { 
        showNavButtons(); 
       } 
      if(move!=null || back!=null) { 
       move.setEnabled(true); 
       back.setEnabled(true); 
      } 

2方法:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
                 int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

    public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 

     return inSampleSize; 
    } 

答えて

0

私は何もしませんでした。私はこのプロジェクトを3〜4回改造しましたが、助けにはなりませんでした。しかし、私はちょうど変更なしで今すぐ再構築し、それは働いた。

+1

あなたの答えには誓わないでください。 – JAL

+0

アプリで画像を操作するときに、プロジェクトをきれいにする/再構築する必要があるようです。私はアプリに3つの画像を含んでいた(彼らは〜1.5MBを一緒に持っていた)とアプリがクラッシュした。私が何をしても(私は50kbにサイズを縮小することができました)、それは助けになりませんでした。私がプロジェクトをきれいにする/再構築した後、すべてが突然働いた。 私は、キャッシュされたイメージがclean/rebuildの前に使われていたと思います。 – vanomart

関連する問題