在Android中共享文件后立即删除文件 [英] Delete a file just after it is shared in android

查看:197
本文介绍了在Android中共享文件后立即删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像文件共享给WhatsApp. Whatsapp无法读取私人文件,因此我将其保存到公共目录中.

I'm sharing image file to WhatsApp. Whatsapp can't read private file so I saved it into public directory.

我要->当用户共享图像时,应将其从存储中删除.

I want --> when a user shares the image it should be deleted from the storage.

try {
   String uniqueName = System.currentTimeMillis() + "";
   File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "ABBM-temp");
   if (!tempFile.exists()) {
       tempFile.mkdirs();
   }
   tempFile = new File(tempFile + uniqueName + ".png");

   FileOutputStream fOut = new FileOutputStream(tempFile);
   b.compress(Bitmap.CompressFormat.PNG, 100, fOut);
   fOut.flush();
   fOut.close();

   final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
   intent.setType("image/png");
   startActivity(Intent.createChooser(intent, "Share image via"));
   //tempFile.delete();

} catch (Exception e) {
   e.printStackTrace();
   Toast.makeText(getApplicationContext(), "Image not shared, please grant storage permission", Toast.LENGTH_SHORT).show();
}

上面的代码打开了共享意图,但是在后台删除了文件,我无法共享文件.

The above code opens sharing intent but deletes file in background and I'm unable to share the file.

共享文件并将其删除.

谢谢..

推荐答案

您的代码有些笨拙,并且保证在最新版本的Android中会出现错误.以下是一些观察结果:

Your code is a bit clunky and is guaranteed to have errors in the latest versions of Android. Here are some observations:

我知道您正在写入主存储器,因此其他应用程序可以读取该文件,但不应该那样做.使用文件提供程序并从中获取一个Uri.这里是文档:文件提供商文档.设置好文件提供程序后(相信我,这很容易,您只需在清单中添加一个代码段并添加一个xml路径)就可以将其私有地写入内存.像这样:

I know that you are writing to the main storage, so other apps can read the file, but it shouldn't be done that way. Use a file provider and get a Uri from that. Here is the documentation: File Provider Documentation. When you have your file provider set-up(believe me it's easy, you just have to add a snippet to your manifest and add a xml path) you can write privately to the memory. Like this:

FileOutputStream fOut = openFileOutput("the_name_of_file_here", Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();

然后使用getUriForFile,您可以获取所允许访问的任何应用程序可用的Uri.像这样:

Then using a getUriForFile you can get a Uri usable by any app that you give access to. Like this:

File file = new File(getFilesDir(), "the_name_of_file_here");
Uri uri = getUriForFile(this, "com.yourpackagename.fileprovider", file);

2.使用ShareCompat

设置FileProvider后,我建议您使用ShareCompat而不是常规的共享活动来共享文件. ShareCompat的优点是得到Google的正式支持,您也可以与它共享大量文件.无需将URI_PERMISSION授予其他应用程序.这是一个示例:

2. Use ShareCompat

When you have set-up the FileProvider, I suggest to you that for sharing files use ShareCompat instead of a regular sharing activity. ShareCompat has the advantage of being officially supported by Google and you can also share a multitude of files with it. Without having to grant URI_PERMISSION to the other app. Here's an example:

Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                    .setType(type) //This is the MIME type
                    .setStream(uri) //This is the uri we got earlier
                    .getIntent();
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(shareIntent); //Starting the activity

我建议您在此浏览此博客:获得从Share-Compat 开始了解更多信息.希望这会有所帮助.

I advise you to take a look here at this blog: Getting Started with Share-Compat for more information. Hope this helps.

如果由于偏好或因为它更适合您的应用而仍要使用功能,则可以做一件事:

If you still want to use your function, because of preference or because it suits your app better, here is one thing you can do:

不要使用startActivity(Intent),而要使用startActivityForResult(Intent, int).有了这个,您可以做的是传递一个将作为id的整数值.然后,当文件共享并返回到您的应用程序时,将触发onActivityResult()函数.它将有一个requestCode.像这样:

Instead of using startActivity(Intent) use startActivityForResult(Intent, int). With that what you can do is pass an integer value which will work as an id. Then when the file is shared and you return back to your app, the onActivityResult() function will be fired. It will have a requestCode. Like this:

//Create your share intent as you do in your code, then:
startActivityForResult(intent, 512); //I passed 512 you can pass anything you want

然后覆盖此功能:

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    
    //Check if it is from the same code, if yes delete the temp file
    if(requestCode == 512) {
       tempFile.delete();
    }
}

这篇关于在Android中共享文件后立即删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆