我試圖通過Android的ShareActionProvider共享一個下載的位圖,並且我遇到了實際將位圖傳遞給適當的應用程序(如Messenger,Google +,Gmail)的問題。當我將意圖傳遞給uri時,沒有任何反應(圖像未在第三方應用程序中填充)。與ShareActionProvider共享一個圖像
這裏是我的意圖:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
writeToDirectory(bitmap, "cached-image.png"); // write bitmap to file system in order to share
intent.putExtra(Intent.EXTRA_STREAM, getFileUri("cached-image.png"))
mShareActionProvider.setShareIntent(intent);
我目前保存位圖使用下列到文件:
public String writeToDirectory(Bitmap bitmap, String filename) {
assert context != null;
assert bitmap != null;
assert filename != null;
// Ensure directory exists and create if not
String path = Environment.getExternalStorageDirectory() + File.separator + "myApp";
File f = new File(path);
boolean dirExists = f.isDirectory();
if (!f.isDirectory() && !f.exists()) {
dirExists = f.mkdirs();
}
if (dirExists) {
File file = new File(sharedPrivateExternalPath, filename);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (IOException ex) {
e(TAG, "Write failed!");
e(TAG, ex.getMessage());
ex.printStackTrace();
}
return file.toString();
} else {
return "";
}
}
,我用這種方法
public Uri getFileUri(String filename) {
assert context != null;
assert filename != null;
String path = Environment.getExternalStorageDirectory() + File.separator + "myApp";
File file = new File(path, filename);
return Uri.fromFile(file);
}
檢索烏里
我已檢查文件是否寫入相應的地方(Uri是file:///storage/emulated/0/myApp/cached-image.png
),並能夠在那裏查看圖像(從設備上執行了adb pull
),儘管圖像沒有通過。我在日誌中沒有看到任何錯誤(沒有FileNotFoundException或任何類型的錯誤)。這是文件權限問題嗎?我無法分享到「非公開」的位置嗎?
如果我將
getExternalStorageDirectory() + File.separator + "myApp";
更改爲普通
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
它可以很好地工作......但它困擾着我,它不能以另一種方式工作:)。
任何幫助將是偉大的!
我不明白的問題,你需要共享圖像路徑還是什麼? –
對不起,問題有點含糊。我更新了我的問題,但我也發現我的問題。 – loeschg