gpt4 book ai didi

java - Android:无法添加附件: `SecurityException when openAssetFileDescriptor`

转载 作者:行者123 更新时间:2023-12-02 10:22:26 24 4
gpt4 key购买 nike

我无法在 native Android 电子邮件或 Gmail 应用程序中附加我的文件。

Gmail 给我这个错误:


2019-01-17 16:33:17.884 15415-15415/? E/Gmail: Gmail:Error adding attachment
fjd: SecurityException when openAssetFileDescriptor.

这是将文件保存在存储上的代码:

 public Uri getUrlFromDrawable(String base64ImageData) {

FileOutputStream fos;
try {

final String pureBase64Encoded = base64ImageData.substring(base64ImageData.indexOf(",") + 1);
final byte[] decodedBytes = Base64.decode(pureBase64Encoded, Base64.DEFAULT);
String filename = "receipt2_" + System.currentTimeMillis() + ".jpg";
File file = new File(reactContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
fos = new FileOutputStream(file);
fos.write(decodedBytes);
fos.flush();
fos.close();

Uri contentUri = FileProvider.getUriForFile(getReactApplicationContext(), "com.myapp.fileprovider", file);
return contentUri;
}


return null
}

这是我发送邮件的函数:

 public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));

if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}

if (options.hasKey("body") && !options.isNull("body")) {
String body = options.getString("body");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder().append(body).toString())
);

}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
ReadableArray recipients = options.getArray("recipients");
i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
}

if (options.hasKey("receipt") && !options.isNull("receipt")) {
String base64ImageData = options.getString("receipt");

try {
if (base64ImageData != null) {
Uri imageUri = getUrlFromDrawable(base64ImageData);
i.putExtra(Intent.EXTRA_STREAM, imageUri);

}

} catch (Exception e) {
Log.e("ERROR", "Cannot save receipt");
}


}

PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);


if (list == null || list.size() == 0) {
callback.invoke("not_available");
return;
}

if (list.size() == 1) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(i);
} catch (Exception ex) {
callback.invoke("error");
}
} else {
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
callback.invoke("error");
}
}
}

这是文件provider.xml

   <?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="Pictures"
path="/" />
</paths>

这是`AndroidManifest.xml

中的 provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
tools:replace="android:authorities"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
tools:replace="android:resource"
android:resource="@xml/fileprovider" />
</provider>

最佳答案

我设法通过像这样在运行时授予权限来修复它

  List<ResolveInfo> list =  manager.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : list) {
String packageName = resolveInfo.activityInfo.packageName;
reactContext.grantUriPermission(packageName, imageUri , Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

关于java - Android:无法添加附件: `SecurityException when openAssetFileDescriptor`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54238900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com