【问题标题】:How to solve Garbage Collection error with Picasso in Android如何在 Android 中使用 Picasso 解决垃圾收集错误
【发布时间】:2015-12-15 19:27:53
【问题描述】:

我目前正在使用 Picasso 从服务器端加载图像并将其保存在 Android 的内部存储中。 我正在使用以下代码从服务器端加载图像:

Handler uiHandler = new Handler(Looper.getMainLooper());
            uiHandler.post(new Runnable() {
                @Override
                public void run() {

                   System.out.println("start run.....");
                    Picasso.with(context)
                            .load(url)
                            .resize(10, 10)
                            .into(new Target() {


                                @Override
                                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                    System.out.println("start picasso....");

                                    if (bitmap != null) {
                                        // save image in internal memory

                                        String directory = saveToInternalStorage(bitmap, name);
                                        System.out.println(directory);

                                    } else
                                    System.out.println("image return is null.....");



                                }

                                @Override
                                public void onBitmapFailed(Drawable errorDrawable) {


                                    System.out.println("Failure in loading photo from server: " + name);


                                }

                                @Override
                                public void onPrepareLoad(Drawable placeHolderDrawable) {

                                }


                            });

以及以下代码将图像保存在内存中:

private String saveToInternalStorage(Bitmap bitmapImage, String imageName){
    System.out.println("start saving image......");
    ContextWrapper cw = new ContextWrapper(context);

    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,imageName);

    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        boolean save=bitmapImage.compress(Bitmap.CompressFormat.PNG, 2, fos);
        System.out.println(save);
        fos.flush();
        fos.close();
    } catch (Exception e) {
       System.out.println("Error in saving photo "+e.toString());
    }
    System.out.println("Image is successfully saved..."+directory.getAbsolutePath());
    return directory.getAbsolutePath();
}

但是,我的问题在于垃圾收集。毕加索根本没有启动,即使我没有收到毕加索的任何失败消息,我也面临以下错误:

D/dalvikvm:GC_CONCURRENT释放434K,11%释放12767K/14215K,暂停14ms+25ms,共118ms

如果有人建议我任何解决方案来避免此错误,我将不胜感激。

【问题讨论】:

    标签: android garbage-collection picasso


    【解决方案1】:

    这不是错误,而是来自 GC 的信息,表明 GC_CONCURRENT 已释放 434K 的内存并花费了 25ms。有Google IO talk 我推荐你看it

    “毕加索根本无法启动”。您根本无法加载图像吗?

    我不确定您是否在加载图像或保存到内部存储方面遇到任何问题。我猜你能做到,你认为错误的消息是你的问题。

    如果我没有做对。我保证你以更具体的方式起草你的问题。

    希望这会有所帮助!

    【讨论】:

    • 我使用 System.out.println("start picasso...."), for onBitmapLoaded of Picasso,但它不打印任何东西。但是在我的 REST 服务上,我可以理解该图像被调用。
    • 让我明确两点。 1)你能加载图像吗? 2)它没有进入 onBitmapLoaded 吗?你试过断点调试吗?
    • 我的意思是毕加索与服务器建立连接,但也许垃圾收集不允许将图像加载到手机中。我在调试中遇到以下消息:WAIT_FOR_CONCURRENT_GC 阻塞 0ms
    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多