如何解决Android中Picasso的垃圾收集错误

时间:2015-12-15 10:35:47

标签: android garbage-collection picasso

我目前正在使用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

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

1 个答案:

答案 0 :(得分:0)

这不是错误,而是来自GC的信息,说GC_CONCURRENT释放了434K的内存并耗时25ms。有谷歌IO谈话我建议你看it

“毕加索根本没有开始”。你根本无法加载图片吗?

我不确定您是否在加载图像或保存到内部存储空间方面遇到任何问题。我可以猜到你能够做到这一点,你认为错误的信息是你的问题。

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

希望这会有所帮助!