假設我有一個保存文件saves.xyz
,並且我想通過郵件共享它。使用Intent.ACTION_SEND發送重命名的文件
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);
}
}
我會很感激,如果我要在這裏啓發。
'我現在想要能夠重命名附件'。多麼大驚小怪。首先重命名文件。然後附上它。 – greenapps
發送意圖沒有指定名稱。 – greenapps
@greenapps也許我不清楚。我希望附加文件具有不同的名稱,但我不想更改我的文件名,也沒有剩餘文件 – Irhala