当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?
示例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)
日志:
android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外
编辑:
在定位Android Nougat时,file://不再允许使用URI.我们应该使用content://URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?
我的应用程序创建带附件的邮件,并使用意图Intent.ACTION_SEND来启动邮件应用程序.
它适用于我测试过的所有邮件应用,除了新的Gmail 5.0(适用于Gmail 4.9),邮件打开时没有附件,显示错误:"附件权限被拒绝".
在logcat上没有来自Gmail的有用消息.我只测试了Android KitKat上的Gmail 5.0,但是在多个设备上.
我为附件创建了这样的文件:
String fileName = "file-name_something_like_this";
FileOutputStream output = context.openFileOutput(
fileName, Context.MODE_WORLD_READABLE);
// Write data to output...
output.close();
File fileToSend = new File(context.getFilesDir(), fileName);
Run Code Online (Sandbox Code Playgroud)
我知道安全问题MODE_WORLD_READABLE.
我发送这样的意图:
public static void compose(
Context context,
String address,
String subject,
String body,
File attachment) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(
Intent.EXTRA_EMAIL, new String[] { address });
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.fromFile(attachment));
Intent chooser = Intent.createChooser(
emailIntent,
context.getString(R.string.send_mail_chooser));
context.startActivity(chooser);
}
Run Code Online (Sandbox Code Playgroud)
在创建文件或发送意图时,我有什么问题吗?有没有更好的方法来启动带附件的邮件应用程序?或者 …
我正在共享一个图像,这段代码适用于Android 6之前的设备:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(new File(mFilename));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
mContext.startActivity(Intent.createChooser(shareIntent, mChooserTitle));
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用Android 6共享时,我收到toast错误" 无法附加空文件 ".
我确认该文件存在且不是零长度.
有人有解决方案吗?