获取图像的URI毕加索? [英] Get image uri from picasso?

查看:242
本文介绍了获取图像的URI毕加索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的列表图像的URL,我使用加载了一个 ViewPager 使用毕加索。我需要能够提供共享功能,这些图像通过一个意图(通过<一个最终分享href="http://developer.android.com/guide/topics/ui/actionbar.html#ShareActionProvider">ShareActionProvider).从我读过,毕加索是不是真的用来处理这种事情的开箱即用,但它提供了所有必要的工具来做到这一点。

I have a fairly large list of image URL's that I'm using to load up a ViewPager using Picasso. I need to be able to provide sharing capabilities for these images via an intent (ultimately sharing via ShareActionProvider). From what I've read, Picasso isn't really built to handle this sort of thing out of the box, though it provides all the necessary tools to do so.

我的研究之前的计划是建立一个简单的 LruCache ,它使用URL作为键和位图值。此缓存将通过毕加索的目标接口出现在 onBitmapLoaded 。每当我想分享一个形象,我会检查缓存为位图。如果它不存在,我就取与毕加索。现在,我有一个缓存的位图不管,我会写一个文件(...这部分看起来不正确,但我必须写一个文件来获得一个URI,对吧?)和文件URI添加到意图。

My plan before research was to create a simple LruCache which uses the url as the key and bitmap value. This caching would occur in onBitmapLoaded via Picasso's Target interface. Whenever I want to share an image, I'll check the cache for the bitmap. If it's not there, I'll fetch with Picasso. Now that I have a cached bitmap regardless, I'll write to a file (...this part doesn't seem right, though I have to write to a file to get a uri, right?) and add the file uri to the intent.

不过,我看到的 Picasso.Builder 我可以设置(并保留提及)我自己的高速缓存 - 的 http://stackoverflow.com/a/18552559/413254 。这意味着我可以自定义目标和混乱正确实施散code 和<$ C做掉$ C>等于的方法,以确保准确回收,检索等。

However I see that with the Picasso.Builder I can set (and retain a reference to) my own cache - http://stackoverflow.com/a/18552559/413254. This means I could do away with the custom Target and confusion with properly implementing hashCode and equals methods to ensure accurate recycling, retrieval, etc.

我的问题是,如何毕加索使用这个缓存?的关键是什么?有没有办法让一个位图开放的,而不将其写入磁盘?

My question is, how does Picasso use this cache? What are the keys? Is there a way to get a bitmap Uri without writing it to disk?

推荐答案

如果你想使用 ShareActionProvider 共享图像中的当前页面,您不必对保持你自己的图像缓存。但是,为了能够将其分享给他人,图像应在设备上的共享文件系统。

If you want to use ShareActionProvider to share the image on the current page you don't have to keep your own cache of images. But to be able to share it to others, the image should be in the shared file system on the device.

如果您使用的图像加载库,如环球图片自定义磁盘缓存的支持会更好装载机

It would be better if you use image loading libraries with custom disk cache support like Universal Image Loader

如果你想使用毕加索(这是一个很好的决定)。

If you want to use Picasso (which is a good decision).

您要么必须保存图像的副本,在每一页上的变化是,也不是一个好的选择。

You either have to save a copy of the image on every page change which is nor a good option.

或者你可以给一个自定义的网络处理器毕加索和设置自定义缓存实现它。我会建议使用OkHttp自定义缓存存储在您需要的格式文件。当你这样做,你必须有一个功能,将图像的URL文件在设备上的路径。

Or you can give a custom network handler to Picasso and set a custom cache implementation to it. I would suggest using OkHttp with custom caching which stores files in the format you desire. When you do that, you have to have a function that converts image URLs to file path on a device.

在每个页面的变化,如果你有一个片段里面的 ViewPager ,把 ShareActionProvider 到您的片段。

In every page change, if you have a Fragment inside your ViewPager, put the ShareActionProvider into your Fragments.

获得 ShareActionProvider 里面的 onCreateOptionsMenu 的参考。然后设置你的文件路径的意图。

Get the reference of the ShareActionProvider inside onCreateOptionsMenu. And then set the Intent with the file path you get.

Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(Utils.getFilePath(imageUrl));
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
mShareActionProvider.setShareIntent(intent);

编辑: 另一种选择我要preFER是沟 ShareActionProvider 和使用普通的菜单项这一点。与 ShareActionProvider 的问题是,你必须使共享意图为用户准备的前手共享。你必须要准备好,即使用户将不会分享。

The other option I would prefer is to ditch ShareActionProvider and use a normal menu item for this. The problem with ShareActionProvideris that you have to make the share Intent ready for user to share before hand. You have to make it ready even the user won't share it.

但是,当你有一个正常的按钮,它是非常容易,因为你只能使操作时,用户点击分享按钮。在这种情况下,你可以简单地请求图像一次从毕加索目标对象,并写出位图你必须在共享外部文件系统中的文件和共享。

But when you have a normal button, it is much easier because you only make the operation when the user clicks the share button. In that case you can simply request the image one more time from Picasso with a Target object and write the Bitmap you got to a file in the shared external file system and share it.

这篇关于获取图像的URI毕加索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆