public void openFile(String filePath, String filename) {
// filePath = /storage/emulated/0/Android/data/XX包名/files/ewx4nwlydwhlca#mcs/ewx4nwlydwhlca#mcs_fc68b27f9b2dc0ed70fc84c990bb8650/file/15/5c67f1fc-9993-4db3-b3f8-3d4467851415.jpg
// filename = Screenshot_20211022_162123_com.tencent.mm.jpg
File file = new File(filePath);
if (!file.exists()) {
return;
}
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 设置intent的Action属性
intent.setAction(Intent.ACTION_VIEW);
// 获取文件file的MIME类型
String type = CommonUtils.getMIMEType(file); // image/jpeg
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
Uri uri = FileProvider.getUriForFile(this,BuildConfig.APPLICATION_ID+".fileprovider",file);
intent.setDataAndType(uri, type);
}else {
}
// 跳转
if (intent.resolveActivity(this.getPackageManager()) != null) {
this.startActivity(intent);
} else {
CommonUtils.showToast(this,
this.getString(R.string.cannot_open_this_type_file) + filename);
}
}
manifest中的代码
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"
/>
</provider>
上面的代码 执行完后跳转出去相册后秒关相册 没看到错误日志 有大佬能知道一下吗 我感觉就是Provider的问题 但是我尝试调整后 就发现打开闪退掉 SOS
大概是这样一个代码
最终根据康师傅的进行修改和判断 大致分为这种情况
宝们 我想要的是用uri打开手机内对应的相册内容 似乎不是image/*打开所有相册
为什么要用 内容提供者呢
直接
然后onActivityResult
这用什么手机测的,手机还是模拟器?手机是android 几
暂时发现是这条Action的问题 明天再卷了 球球大佬们看看
你打开的是一个activity,我没看懂,你这是啥流程。
这个内容提供者是你自己的?