简体   繁体   English

如何共享通过Volley下载并缓存的图像?

[英]How to share image that is downloaded and cached with Volley?

In my app I download images from server using Volley and its ImageLoader (with BitmapLruCache). 在我的应用程序中,我使用Volley及其ImageLoader(带有BitmapLruCache)从服务器下载图像。 I want to create a share option, researched a little bit, and found that share intent can only share localy stored images. 我想创建一个共享选项,进行了一些研究,发现共享意图只能共享本地存储的图像。 First question, is this right? 第一个问题,对吗?

Second, if that is right, what should I do? 第二,如果那是正确的,那我该怎么办? Should I download image again and save it to local storage? 我应该再次下载图像并将其保存到本地存储吗? Or put it in MediaStore? 还是放在MediaStore中? Or I can pull image from cache and share cached version? 或者我可以从缓存中提取图像并共享缓存的版本? What are the best practices? 最佳做法是什么?

Any suggestion is welcome, or code snippet. 欢迎任何建议或代码段。

What I did is made another request for getting the image, and added an ImageListener which provides ImageContainer containing bitmap. 我所做的是再次请求获取图像,并添加了一个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();
                }
            }
        });

As image is already downloaded and cached (using BitmapLRU cache and ImageLoader), this new request gives me bitmap from cache so no new network data is made. 由于已经下载并缓存了图像(使用BitmapLRU缓存和ImageLoader),因此此新请求为我提供了来自缓存的位图,因此不会产生新的网络数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取使用Volley ImageLoader下载的缓存位图? - How to I get the cached Bitmap I downloaded using Volley ImageLoader? 如何将凌空下载的图像发送到下一个活动 - How to send an image downloaded by volley to the next activity Volley NetworkImageView清除缓存的图像 - Volley NetworkImageView clear cached image 如何分享从flutter下载的图片 - How to share image downloaded from flutter 如何通知应用程序已下载并缓存的图像已在服务器上更新/更改,需要重新下载? - How to inform the app that an image that was already downloaded and cached, was updated/changed on the server and needs to be Re-downloaded? 通过Intent快速共享Fresco缓存的图像 - Quick Share Fresco cached image Via Intent 以编程方式从凌空下载的图像设置背景布局 - Set background layout programmatically from image downloaded with volley 如何访问 Picasso 的缓存图像以进行共享意图? - how can I access Picasso' s cached image to make a share intent? 如何使用截击将下载的文本发送到下一个活动 - how to send downloaded text using volley to the next activity 如何处理从服务器下载,缓存和显示的图像? Android的 - How to handle images being downloaded, cached, and displayed from the server? Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM