android - 如何共享使用 Volley 下载和缓存的图像?

标签 android caching android-volley

在我的应用程序中,我使用 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/

相关文章:

Android AVD 问题

android - "This app won' t 运行直到您更新 Google Play 服务“GameBaseUtils

.net - 客户端缓存 C#

data-structures - 无缓存数据结构和动态语言——有效吗?

android - 在 Android 上使用 volley 嵌套 JSON

java - RecyclerView 并不总是显示结果

android - 修复基于磁盘的缓存大小的最佳方法是什么?android -volley

java - 运行时 Google map View 错误 - Droid

android - Appcelerator 可以访问 Phonegap 不能访问的 Apple 和 Android 设备的哪些 native API?

Symfony 3.1 缓存 - DeleteItem 在产品中不起作用