回答
我希望你能理解我的问题:-)
对不起,我的延迟,我发现了一个解决方案,但不是一个好...
首先,我真的搜查了一会儿,看了毕加索的代码。看起来你应该提供你自己的下载器和其他东西。但是,为什么我应该使用lib ...
然后,我想这是毕加索的设计/架构,只是缓存文件在内部存储。也许是因为外部存储并不总是可用的(比如用户可能会将他的SD卡插入到他的计算机中),或者可能是因为外部存储没有内部那么快......这是我的猜测。总之,其他应用程序无法访问当前应用程序的内部存储,因此共享无法完成。
因此,我做了一个非常普通的解决方案。我只是等待毕加索给Bitmap
,并将其压缩到外部文件中的文件,然后进行分享。这似乎是一个不好的解决方案,但它真的解决了这个问题,是的...
你应该知道外部缓存目录是否可用。如果没有,你不能分享。你需要把压缩任务放在后台线程中,等待缓存的外部文件......这看起来像是一个不好的解决方案吗?我想是这样...
下面是我的项目的代码,你可以试试...
private boolean mSaved; // a flag, whether the image is saved in external storage
private MenuItem mShare;
private Intent mIntent;
private ShareActionProvider mShareActionProvider;
private File mImage; // the external image file would be saved...
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream os = null;
try {
String dir = CatnutUtils.mkdir(getActivity(), Constants.FANTASY_DIR); // check the exteral dir avaiable or not...
String[] paths = Uri.parse(mUrl).getPath().split("/");
mImage = new File(dir + File.separator + paths[2] + Constants.JPG); // resoleve the file name
} catch (Exception e) { // the external storage not available...
Log.e(TAG, "create dir error!", e);
return;
}
try {
if (mImage.length() > 10) { // > 0 means the file exists
// the file exists, done.
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
return;
}
os = new FileOutputStream(mImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
} catch (FileNotFoundException e) {
Log.e(TAG, "io error!", e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e(TAG, "io closing error!", e);
}
}
}
}
}).start();
mFantasy.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
mFantasy.setImageDrawable(errorDrawable);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {
mFantasy.setImageDrawable(placeHolderDrawable);
}
}
};
@Override
public void onPrepareOptionsMenu(Menu menu) {
mShare.setEnabled(mSaved);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fantasy, menu);
mShare = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShare.getActionProvider();
mShare.setActionProvider(mShareActionProvider);
mShareActionProvider.setShareIntent(mIntent);
}
最后,调用Picasso.with(getActivity()).load(mUrl).into(target);
当文件被保存时,用户可以点击共享菜单做分享。
它似乎可以工作。但在我看来,这是不好的报价。文件正在保存某处。 lib应该给他们开发者。这将是一件好事,而不是增加数百万个功能给毕加索库,增加了非常简单和必要的功能。正因为如此,我放弃了使用毕加索。它具有数百万垃圾功能和非常有限的不错功能 – Adem
我刚刚发现了一个非常好的解决方案本指南。
https://guides.codepath.com/android/Sharing-Content-with-Intents
的代码将是这样的:
// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
// Get access to bitmap image from view
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(ivImage);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
它基本上包括在检索从ImageView的位图,并将其保存到本地临时文件,然后用它来共享。我测试过了,它似乎工作正常。
该解决方案重新压缩位图,浪费内存并损失图像质量 –
是的,它压缩图像。如果它不是你的意图(当时是我的意图),应该很容易删除。 – CarlosRivin
- 1. 毕加索图像缓存
- 2. 毕加索图像加载以前缓存图像
- 3. 如何与WebView共享图像缓存
- 4. 毕加索图书馆是否将图像保存到缓存?
- 5. 如何加载与毕加索aws图像与私人访问
- 6. 毕加索缓存似乎没有我使用的毕加索来处理图像加载和缓存在我的Android Udacity项目合作
- 7. 在后台缓存毕加索图片
- 8. 如何与viewpager共享图像意图?
- 9. 图像缓存离子2打字像毕加索
- 10. 如何把毕加索下载的图像作为额外的意图
- 11. 使用FileProvider共享缓存的图像
- 12. 毕加索:从磁盘加载缓存图像太慢
- 13. 毕加索在无网络时无法加载缓存图像
- 14. Android共享意图共享图像不存在
- 15. 如何从毕加索的缓冲存储器中检索图像?
- 16. 处理图像缓存的正确方法毕加索与MvvmCross
- 17. 毕加索不缓存磁盘上的图像
- 18. 毕加索在appwidget中使用缓存图像的数据
- 19. 如何从毕加索在位图变量中存储图像
- 20. 毕加索重复图像
- 21. 保存图像在SD卡后毕加索加载图像
- 22. 添加另存为图像共享意图
- 23. 试图与意图共享图像Android
- 24. 毕加索缓存例外
- 25. 毕加索缓存清除
- 26. 毕加索磁盘缓存
- 27. 毕加索磁盘缓存不工作
- 28. Android共享意图问题
- 29. 如何存储由毕加索在android下载的图像?
- 30. 毕加索图书馆和GridView图像
您可以截取该视图的位图对象,然后保存在目录中并使用Uri.parse()分享它; – Piyush
有没有一种方法来实现它?我不想将它保存到另一个目录中。 – longkai
意味着你已经将它保存在目录中了吗? – Piyush