在LruCache.java中的mapValue上获取null尝试从URL共享图像

时间:2018-09-14 04:40:05

标签: android picasso

您好朋友,我正在尝试共享url中的图片,我正在使用picasso。在某些情况下,它正在工作,而在另一些情况下却没有。我尝试调试,以下是结果,我得到的是drawable为空,实际上是因为mapValue中的LruCache正在重用null,所以缓存为null,所以{{1 }}为空,因此最终bitmap为空。这是我在调试窗口中看到的:

drawable

key = http://aaa.com/post/img_5egg4c272a.jpg; mapValue = null ;missCount = 5

下面是我分享图片的代码

private final int maxSize;(maxsize: 38347922mb) as visible in debug

private int size; (size:2792880mb) as visible in debug

Lrucache代码:

public void postShare( Item item) {

    String shareText = "";

    Intent shareIntent = new Intent(ACTION_SEND);

    shareIntent.setType("text/plain");

    if (item.getPost().length() > 0) {
        shareText = item.getPost();
    } else {
        if (item.getImgUrl().length() == 0) {
            shareText = item.getLink();
        }
    }

    shareIntent.putExtra(EXTRA_SUBJECT, (String) context.getString(app_name));
    shareIntent.putExtra(EXTRA_TEXT, shareText);

    e("Share without Image");

    if ((item.getImgUrl().length() > 0) && (ContextCompat.checkSelfPermission(this.context, WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED)) {

        e("Share with Image");

        shareIntent.setType("image/*");

        final ImageView image;

        image = new ImageView(context);

        with(context)
                .load(item.getImgUrl())
                .into(image, new Callback() {
                    @Override
                    public void onSuccess() {
                        e("Image Load Success");
                    }

                    @Override
                    public void onError() {
                        e("Image Load Error");
                        image.setImageResource(profile_default_photo);
                    }
                });

        Drawable mDrawable = image.getDrawable();

        Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();

        String file_path = getExternalStorageDirectory() + separator + APP_TEMP_FOLDER;

        File dir = new File(file_path);
        if (!dir.exists()) dir.mkdirs();

        File file = new File(dir, "share.jpg");

        try {

            FileOutputStream fos = new FileOutputStream(file);

            mBitmap.compress(JPEG, 100, fos);

            fos.flush();
            fos.close();

        } catch (FileNotFoundException e) {

            makeText(context, "Error occured. Please try again later.", LENGTH_SHORT).show();

        } catch (IOException e) {

            e.printStackTrace();
        }

        Uri uri = getUriForFile(context, APPLICATION_ID + ".provider", new File(getExternalStorageDirectory() + separator + APP_TEMP_FOLDER + separator + "share.jpg"));

        shareIntent.putExtra(EXTRA_STREAM, uri);
    }

    shareIntent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);

    context.startActivity(createChooser(shareIntent, "Share post"));
}

0 个答案:

没有答案