在我的应用程序中,我使用 Volley 及其 ImageLoader(带有 BitmapLruCache)从服务器下载图像。我想创建一个共享选项,研究了一下,发现共享 Intent 只能共享本地存储的图像。第一个问题,这样对吗?
第二,如果那是对的,我该怎么办?我应该重新下载图像并将其保存到本地存储吗?还是放在MediaStore里?或者我可以从缓存中提取图像并共享缓存版本?最佳做法是什么?
欢迎任何建议或代码 fragment 。
最佳答案
我所做的是再次请求获取图像,并添加了一个 ImageListener,它提供包含位图的 ImageContainer。
MyApplication.getImageLoader(activity).get(imageURL, new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {}
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap mBitmap = response.getBitmap();
ContentValues image = new ContentValues();
image.put(Media.MIME_TYPE, "image/jpg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, image);
try {
OutputStream out = getContentResolver().openOutputStream(uri);
boolean success = mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
if (!success) {
} else {
imageUri = uri;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
Intent shareIntent = ShareCompat.IntentBuilder.from(activity).setType("image/jpg").setText(shareText).getIntent();
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
mShareActionProvider.setShareIntent(shareIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
由于图像已经下载并缓存(使用 BitmapLRU 缓存和 ImageLoader),这个新请求从缓存中为我提供位图,因此不会产生新的网络数据。
关于android - 如何共享使用 Volley 下载和缓存的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795525/