【发布时间】:2019-04-05 09:07:16
【问题描述】:
我正在构建一个允许用户保存位图或在不保存的情况下共享它的应用程序。第二个功能不太好用。我知道应用程序需要先将文件保存到设备上,然后才能在社交媒体应用程序上共享,所以我的想法是,在文件成功共享后立即自动从设备中删除文件。我已经构建了一个删除方法,尝试了 2 种不同的方法,但都没有奏效:
第一种方法:
public void deleteFile(String path){
File file = new File(path);
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
}
第二种方法:
public void deleteFile(String path){
File file = new File(path);
boolean deleted = file.delete();
}
我正在通过共享方法调用deleteFile(String):
public void shareMeme(Bitmap bitmap) {
String path = MediaStore.Images.Media.insertImage(Objects.requireNonNull(getContext()).getContentResolver(), bitmap, "Meme", null);
Uri uri = Uri.parse(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));
deleteFile(path);
}
【问题讨论】:
标签: android file android-intent android-bitmap mediastore