共享临时文件的Android问题
我正在尝试创建一个临时文件并共享它。 所以我创建了这个类:共享临时文件的Android问题,android,temporary-files,share-intent,sharefile,Android,Temporary Files,Share Intent,Sharefile,我正在尝试创建一个临时文件并共享它。 所以我创建了这个类: public class GenerateFile { public static File writeToFile(Context mcoContext, String sBody) { String fileName = "LOG FILE_" + String.valueOf(System.currentTimeMillis()) +".txt"; File file = new Fil
public class GenerateFile {
public static File writeToFile(Context mcoContext, String sBody) {
String fileName = "LOG FILE_" + String.valueOf(System.currentTimeMillis()) +".txt";
File file = new File(mcoContext.getCacheDir(), fileName);
try{
FileWriter writer = new FileWriter(file);
writer.append(sBody);
writer.flush();
writer.close();
return file;
}catch (Exception e){
Toast.makeText(mcoContext, "File write failed: " + e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
要生成我将在此处共享的文件,请执行以下操作:
String logContent = "123";
File filePath = new File(file.getAbsolutePath(), "external_files");
filePath.mkdir();
Uri uri = FileProvider.getUriForFile(StatusActivity.this, getPackageName(), filePath);
Intent intent = ShareCompat.IntentBuilder.from(StatusActivity.this)
.setStream(uri) // uri from FileProvider
.setType("text/html")
.getIntent()
.setAction(Intent.ACTION_VIEW) //Change if needed
.setDataAndType(uri, "text/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
在清单中已经有此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
和提供者声明
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="android.getqardio.com.gmslocationtest"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths类的定义如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="share"
path="external_files"/>
</paths>
但当我试图通过邮件或电报“无法附加文件”或“不受支持的附件”共享时,它会生成消息。在我看来,该文件似乎没有创建。您是否专门向用户询问这些权限?仅仅将目标SDK的权限放在清单中低于28是不够的。另外,在Android Q中,您需要完全绕过外部存储权限,因为这是不允许的 其他应用无法访问您应用的
getCacheDir()
FLAG\u GRANT\u READ\u URI\u PERMISSION
和FLAG\u GRANT\u WRITE\u URI\u PERMISSION
用于内容URI
值,而不是文件URI
值。而且,在Android 7.0+设备上,您的代码应该会出现FileUriExposedException
崩溃
要使您的内容可供其他应用程序使用,请使用FileProvider.getUriForFile()
获取Uri
以放入Intent
,因此我遵循@commonware的建议,编辑了我的代码。这是最终结果:
public class GenerateFile {
public static Uri getFileURI(Context context, String nameFile, String content, String fileExtension) {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
Date date = new Date();
String fileName = dateFormat.format(date)+nameFile+fileExtension;
File file = new File(context.getCacheDir(), fileName);
try{
FileWriter writer = new FileWriter(file);
writer.append(content);
writer.flush();
writer.close();
//Toast.makeText(context, "Writing to the file completed successfully", Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(context, "File writing failed: " + e.toString(), Toast.LENGTH_LONG).show();
}
File filePath = new File(context.getCacheDir(), "");
File newFile = new File(filePath, fileName);
return FileProvider.getUriForFile(context, "MYPACKAGE.fileprovider", newFile);
}
}
在另一个班级:
private void sendFile(String nameFile, String logContent, String fileExtension) {
Uri contentUri = GenerateFile.getFileURI(getApplicationContext(), nameFile, logContent, fileExtension);
Intent intent = ShareCompat.IntentBuilder.from(StatusActivity.this)
.setStream(contentUri) // uri from FileProvider
.setType("text/plain")
.getIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "send"));
}
所以要发送文件。我还删除了清单中的权限(前面提到的),因为我不再需要它了
我还编辑了我的提供者和提供者路径文件,如下所示:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="MYPACKAGE.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<cache-path
name="my_files"
path=""
/>
现在它工作了!非常感谢你们的帮助 我更新了我的代码,所以问:我在清单中插入了提供者,并创建了提供者路径。我还更改了有关文件发送的代码。但不幸的是,现在我遇到了另一个错误:NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)
@TheoldBackbeard:该错误表明getPackageName()
与android.getqardio.com.gmslocationtest
不同。在getUriForFile()
中用于权限名称的值需要与在android:authorities
属性中使用的值匹配。另外,请停止向问题中的源代码中添加“snippet”内容,因为它不是JavaScript,无法运行。@TheOldBlackbeard:另外,您正在getCacheDir()
中保存文件,但您正在元数据中使用
。这些也不匹配。请改用
。抱歉,我不知道如何避免这段代码。非常感谢您的建议,我将尝试编辑代码。