Tôi đã gặp một vài giải pháp không giải quyết được vấn đề này.
Đây là giải pháp phù hợp với tôi. Một lưu ý là bạn cần phải lưu trữ hình ảnh ở một vị trí riêng tư hoặc không phải ứng dụng riêng tư (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
Nhiều đề xuất lưu trữ ở vị trí bộ nhớ cache riêng tư, nhưng điều này không thể truy cập được thông qua các ứng dụng bên ngoài khác, bao gồm Mục đích chung của Tệp Chia sẻ đang được sử dụng. Khi bạn thử điều này, nó sẽ chạy nhưng ví dụ dropbox sẽ cho bạn biết các tập tin không còn có sẵn.
/* BƯỚC 1 - Lưu tệp bitmap cục bộ bằng cách sử dụng chức năng lưu tệp bên dưới. */
localAbsoluteFilePath = saveImageLocally(bitmapImage);
/* Bước 2 - Chia sẻ đường dẫn tập tin tuyệt đối tin không đến thư mục chia sẻ ý định */
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);
}
/* SAVE IMAGE CHỨC NĂNG */
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();
}
/* BƯỚC 3 - Xử lý Share File Intent result. Cần tập tin từ xa tạm thời vv */
@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;
}
//...
}
Tôi hy vọng rằng sẽ giúp một ai đó.
Câu hỏi mới nên được thực hiện riêng biệt vì bạn không thể trao nhiều câu trả lời cho một câu hỏi. Nhưng dù sao, File có một phương thức mkdirs() có thể được sử dụng để đảm bảo thư mục được chỉ định tồn tại. – AlbeyAmakiir
oh ok cảm ơn @AlbeyAmakiir! Lưu ý cho các bài viết trong tương lai! – dabious