我使用这种方法将图像保存到应用程序内部存储中的我的文件夹中。
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:如何打开图库以显示内部存储“文件”文件夹中的所有图像?