我遇到了一些解決方案,它們沒有解決這個問題。
這是一個適合我的解決方案。有一個問題是你需要存儲在共享或非應用私人的位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
許多建議說,在Apps
「私有」緩存位置存儲圖像,但這當然不是通過其他外部應用程序,包括accessable正在使用的通用共享文件意圖。當你嘗試這個,它會運行,但例如dropbox會告訴你該文件不再可用。
/*第1步 - 使用下面的文件保存功能在本地保存位圖文件。 */
localAbsoluteFilePath = saveImageLocally(bitmapImage);
/*第2步 - 分享非私人文件絕對路徑共享文件意圖*/
if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(localAbsoluteFilePath);
File file = new File(phototUri.getPath());
Log.d("file path: " +file.getPath(), TAG);
if(file.exists()) {
// file create success
} else {
// file create fail
}
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION);
}
/*保存圖像功能*/
private String saveImageLocally(Bitmap _bitmap) {
File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
File outputFile = null;
try {
outputFile = File.createTempFile("tmp", ".png", outputDir);
} catch (IOException e1) {
// handle exception
}
try {
FileOutputStream out = new FileOutputStream(outputFile);
_bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
// handle exception
}
return outputFile.getAbsolutePath();
}
/*步驟3 - 處理共享文件意圖結果。需要遠程臨時文件等。*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents
if (requestCode== Navigator.REQUEST_SHARE_ACTION) {
// delete temp file
File file = new File (localAbsoluteFilePath);
file.delete();
Toaster toast = new Toaster(activity);
toast.popBurntToast("Successfully shared");
}
}
/* * UTILS/
public class Utils {
//...
public static File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file =
new File(Environment.getExternalStorageDirectory(), albumName);
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
//...
}
我希望可以幫助別人。
應該單獨提出一個新問題,因爲您無法爲單個問題授予多個答案。但無論如何,File有一個mkdirs()方法可用於確保指定的文件夾存在。 – AlbeyAmakiir
好的,謝謝@AlbeyAmakiir!留意未來的帖子! – dabious