【发布时间】:2023-04-05 00:45:02
【问题描述】:
假设我有一个保存文件saves.xyz,我想通过邮件分享它。
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, content.getShareSubject());
intent.putExtra(Intent.EXTRA_TEXT, content.getShareText());
File savesFile = new File(Environment.getExternalStorageDirectory(), filePath);
Uri savesUri = Uri.fromFile(savesFile);
intent.putExtra(Intent.EXTRA_STREAM, savesUri);
String fileType = activity.getString(R.string.saves_file_type); //"text/xyz"
intent.setType(fileType);
String chooserMessage = activity.getString(R.string.saves_chooser_message);
activity.startActivity(Intent.createChooser(intent, chooserMessage));
这很好用。
不过,我现在希望能够重命名附件(比如saves-01-01-2017.xyz)。
我考虑过复制文件,发送它,然后删除它,但之后没有找到彻底删除它的方法(请参阅this)。
我也尝试实现我自己的FileProvider(建议here)并将saves-*.xyz 重定向到saves.xyz,但我无法让FileProvider 工作(我可能没有掌握他们的充分发挥作用):
manifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mydomain.myapp"
>
...
<application
...
>
<provider
android:name="com.mydomain.myapp.MyFileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"
/>
</provider>
...
</application>
</manifest>
file_provider_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="saves_folder"
path="/"
/>
</paths>
MyFileProvider 类
public class MyFileProvider extends FileProvider {
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
//doesn't print anything
Log.d(MyFileProvider.class.getSimpleName(), uri.toString());
return super.openFile(uri, mode);
}
}
如果我能在这里得到启发,我将不胜感激。
【问题讨论】:
-
I now want to be able to rename the attached file。什么大惊小怪。首先重命名文件。然后附上它。 -
发送意图不需要指定名称。
-
@greenapps 也许我不清楚。我希望附件具有不同的名称,但我不想更改我的文件名,也不想有剩余文件
标签: android android-intent android-contentprovider