2016-12-20 4 views
-2

イメージスライダーを作成していますが、これは何とか私が使用している画像のために起こることが分かっていました。私は試しましたが、android:largeHeap="true"があります。Java.lang.outofメモリエラー

ここ

は画像

public class EsquireAdapter extends PagerAdapter { 

    private int [] imgs = { R.drawable.conf4,R.drawable.food,R.drawable.food1,R.drawable.food2,R.drawable.conf4,R.drawable.food,R.drawable.food1,R.drawable.food2,R.drawable.escalope,}; 
    private LayoutInflater inflater; 
    private Context ctxt; 

    public EsquireAdapter(Context ctxt) { 
     this.ctxt = ctxt; 
    } 


    @Override 
    public int getCount() { 
     return imgs.length; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     inflater = (LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.swipee,container,false); 
     ImageView img = (ImageView)v.findViewById(R.id.ImageView); 
     TextView tv = (TextView)v.findViewById(R.id.textView); 
     img.setImageResource(imgs[position]); 

     container.addView(v); 
     return v; 

    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.invalidate(); 
    } 
} 

を出力し、ここで私のlogcatである私のアダプターのコードです:

FATAL EXCEPTION: main Process: com.example.moses.ngongapp, PID: 21333 java.lang.OutOfMemoryError: Failed to allocate a 144764940 byte allocation with 16777120 free bytes and 27MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)

+2

を動作するかどうか私に教えてください。ヒープダンプを生成し、メモリをどこで使用しているのかを確認してください。 –

+0

Plzアクティビティコードを追加してください。 –

+0

リストビューでdrawableが多すぎます。この問題が発生したのはなぜですか。 – Noorul

答えて

1

は、私はあなたがこのライブラリはあなたのGradleファイル

dependencies { 
    compile 'com.github.bumptech.glide:glide:3.7.0' 
} 

に以下を追加使用し、あなたのresフォルダ内にdrawable-nodpiという名前のフォルダを作成するには、Glideというライブラリを使用することをお勧めします。そこ

をあなたのすべての画像を配置し、あなたのinstantiateItemに次

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    inflater = (LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.swipee,container,false); 
    ImageView img = (ImageView)v.findViewById(R.id.ImageView); 
    TextView tv = (TextView)v.findViewById(R.id.textView); 

    Glide.with(context) 
    .load(imgs[position]) 
    .asBitmap()‌​.transform(new CenterCrop(context)) 
    .diskCacheStrategy(DiskCacheStrategy.NONE) 
    .skipMemoryCache(true) 
    .int‌​o(img) 

    container.addView(v); 
    return v; 

} 

は、それがOOM問題への迅速な答えはあり決してません

+0

ちょっと@MithunSarkerShuvro私は "変換"メソッドで、どのアイデアをどのように処理するには、アローを得る? –

+0

どのようなエラーが表示されますか?あなたが無視することができる方法によって.asBitmap().transform(new CenterCrop(context))この行 –

+0

うわーそれは働いた..良い..私のイメージは現在クラッシュなしでロードされています...しかし、あなたはどのように知っていますか?今は画面の上部に表示されているため、画像を中央に表示させてください。 –

1

のアプリに設けられたメモリは慣れているので、この現象が発生する理由は、ファイルサイズが大きいためにイメージがアップします。
大きなヒープを有効にすることは、この場合は解決策にはなりません。アプリに余分なメモリを与えてしまい、最終的には使い果たされるだけです。あなたがここに試すことができ
ほとんどのソリューションは、あなたのイメージ

  1. スケーリングすることができます:あなたが手動で画像をスケーリングするためのコードを記述したくない場合はhttps://developer.android.com/training/displaying-bitmaps/load-bitmap.html
  2. をここでは簡単tutorial on Picasso

    Picasso
    のようなライブラリを使用します
    Picasso 
    .with(context) 
    .load(UsageExampleListViewAdapter.eatFoodyImages[0]) 
    .fit() 
    // call .centerInside() or .centerCrop() to avoid a stretched image 
    .into(imageViewFit); 
    

ピカソ.fit()メソッドは、あなたのための画像のスケーリングを行います。