PICASSO致命异常:java.lang.OutOfMemoryError:无法分配JNI Env

时间:2019-04-11 04:52:15

标签: java android out-of-memory picasso

我一直都在遇到毕加索的大问题。 我就这样

Fatal Exception: java.lang.OutOfMemoryError: Could not allocate JNI Env
   at java.lang.Thread.nativeCreate(Thread.java)
   at java.lang.Thread.start(Thread.java:729)
   at com.squareup.picasso.Picasso.(SourceFile:205)
   at com.squareup.picasso.Picasso$Builder.build(Picasso.java:864)
   at com.destina.travling.ui.main.fragment.home.restaurant.detail.FoodImageAdapter.onBindViewHolder(FoodImageAdapter.java:58)
   at com.destina.travling.ui.main.fragment.home.restaurant.detail.FoodImageAdapter.onBindViewHolder(FoodImageAdapter.java:27)
   at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
   at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
   at androidx.recyclerview.widget.RecyclerView$Adapter.getItemCount(RecyclerView.java:6781)
   at androidx.recyclerview.widget.RecyclerView$Adapter.onCreateViewHolder(RecyclerView.java:6823)
   at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
   at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
   at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
   at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
   at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
   at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
   at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
   at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
   at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
   at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
   at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
   at android.view.View.measure(View.java:19886)
   at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1248)
   at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1593)
   at android.view.View.measure(View.java:19886)
   at androidx.core.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1502)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
   at androidx.core.widget.NestedScrollView.onMeasure(NestedScrollView.java:556)
   at android.view.View.measure(View.java:19886)
   at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1248)
   at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1593)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
   at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:805)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
   at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
   at com.android.internal.policy.DecorView.onMeasure(DecorView.java:693)
   at android.view.View.measure(View.java:19886)
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2323)
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1414)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1667)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1302)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6441)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
   at android.view.Choreographer.doCallbacks(Choreographer.java:688)
   at android.view.Choreographer.doFrame(Choreographer.java:623)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
   at android.os.Handler.handleCallback(Handler.java:754)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:163)
   at android.app.ActivityThread.main(ActivityThread.java:6238)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

我只是这样修改了毕加索的设置:

OkHttpClient client = HttpsClient.getHttpsClient(context);
        Picasso picasso = new Picasso.Builder(context)
                .downloader(new OkHttp3Downloader(client))
                .memoryCache(new LruCache(getBytesForMem()))
                .build();
        picasso.load(link)
                .fit()
                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                .networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
                .placeholder(R.drawable.ic_broken_image)
                .error(R.drawable.ic_broken_image)
                .into(holder.ivFoodImage);

private int getBytesForMem() {
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager)
                context.getSystemService(ACTIVITY_SERVICE);
        if (activityManager != null) {
            activityManager.getMemoryInfo(memoryInfo);
        }

        double availableMemory = memoryInfo.availMem;
        return (int) (12 * availableMemory / 100);
    }

我使用com.squareup.picasso:picasso:2.71828的piccasso库版本

以前,我放了memoryCache,然后经常遇到该错误,但现在噩梦又来了。

我的问题是

  1. 如何在没有OutOfMemoryError的情况下使毕加索出色地工作?
  2. Picasso.singleton可以解决此问题吗?

我知道,某些用户可能建议尝试另一个库,但是我仍然使用库,因为我的API使用自签名证书来获取API。这就是为什么您可以看到OkHttp与Picasso结合的原因。

但是,如果您一直尝试建议继续使用其他库,也许您可​​以教我如何使用自签名证书来使API使用您推荐的库。

如果有任何问题,我都会放弃投票,谢谢:)。

2 个答案:

答案 0 :(得分:1)

这是对您的请求的简化答复,但我怀疑它是否对您有用:

public class TestAdapter extends RecyclerView.Adapter {

List<Object> items;
Picasso picasso;
Context context;
public TestAdapter(List<Object> inputs, Context in_context) {
    items = inputs;
    context = in_context;
    OkHttpClient client = HttpsClient.getHttpsClient(context);
    picasso = new Picasso.Builder(context)
            .downloader(new OkHttp3Downloader(client))
            .memoryCache(new LruCache(getBytesForMem()))
            .build();

}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //return your ViewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    // some codes here

    picasso.load(link)
            .fit()
            .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
            .placeholder(R.drawable.ic_broken_image)
            .error(R.drawable.ic_broken_image)
            .into(holder.ivFoodImage);
}

@Override
public int getItemCount() {
    return items.size();
}

private int getBytesForMem() {
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = (ActivityManager)
            context.getSystemService(ACTIVITY_SERVICE);
    if (activityManager != null) {
        activityManager.getMemoryInfo(memoryInfo);
    }

    double availableMemory = memoryInfo.availMem;
    return (int) (12 * availableMemory / 100);
}
}

在活动中,使用新的构造函数创建RecyclerView适配器。

//get your recyclerView from your layout as mRecyclerView
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(manager);
TestAdapter adapter = new TestAdapter(your_data,this);
mRecyclerView.setAdapter(adapter);

答案 1 :(得分:0)

您可以使用 Picasso 来解决著名的内存不足错误,
关键是您的内存不足,其他任何库也可能发生

有时大尺寸的图像会导致这种问题
有时在清单中设置大堆可以解决问题 ,...
stackoverflow 中的大量数据内存不足,而 Picasso

重点是您应该照顾好记忆并将其解剖
我建议重点关注堆问题,而不要更改库并面临另一个新问题

不要忘记监视堆并利用堆功能