回答
我希望你能理解我的問題:-)
對不起,我的延遲,我發現了一個解決方案,但不是一個好...
首先,我真的搜查了一會兒,看了畢加索的代碼。看起來你應該提供你自己的下載器和其他東西。但是,爲什麼我應該使用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