android - 尝试从内部存储中读取文件时,图库会立即打开和关闭
问题描述
我使用这种方法将图像保存到应用程序内部存储中的我的文件夹中。
public static String saveToInternalStorage(Context ctx,Bitmap bitmapImage){
Date date = new Date();
File mypath = new File(ctx.getFilesDir(), "ATM_"+date.getTime()+".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return mypath.getAbsolutePath();
}
在应用程序的另一个选项中,我想在 android 库中打开图像,我尝试使用此代码执行此操作
Intent intent = new Intent();
// set flag to give temporary permission to external app to use your FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
File file = new File(path);
if(file.exists() && file.isFile()) {
Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID, file);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
}
我的清单上也有这个声明
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>
我的 file_provider_paths 看起来像这样
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="/"/>
发生的情况是,当我按下按钮打开图库上的图像时,我会看到该文件类型的应用程序选择器,当我选择图库时,它会立即打开和关闭。
问题1:为什么会这样?
问题 2:如何打开图库以显示内部存储“文件”文件夹中的所有图像?
解决方案
推荐阅读
- javascript - 如何在 javascript Object.assign 方法中使用所有函数参数
- .net - IEnumerable.Min() 使用通用对象返回奇怪的结果
- mysql - 我可以选择每一行并限制具有不同值的行吗?
- python - 何修复 playsound excpetion “playbin.set_state 返回
我正在尝试使用该
playsound
模块在 Python 中制作一个简单的节拍器。但是当我运行它时,出现以下错误:Traceback (most recent call last): File "/run/media/mb/Vol
- azure-active-directory - Microsoft Botframework V4 虚拟助手 Azure AD 身份验证
- c - 如何让 C 程序发出 HTTP 请求并读取响应?
- c# - 如何更改一定数量的按钮(x)的不透明度,而不是一一写出来?(C#)
- android - 在 React-Native 中存储不可更改数据的正确方法
- html - Html 日期格式更改为月份日期年份
- xamarin - 如何在适用于 Android 的 Xamarin.Forms 应用程序中使用 D8、R8?