您好朋友,我正在尝试共享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"));
}