我使用glide
库将相机拍摄的图像加载到imageview
。
我已经在3种不同的设备上测试了我的应用
图像在前两个设备中成功加载,但在第三个设备中失败并显示错误:
内容加载失败://com.tsiro.dogvip.provider/p/20170613_215541 大小[324x324];例外Glide产生)。 class com.bumptech.glide.load.engine.GlideException: 无法加载资源 原因(1 of 2):class com.bumptech.glide.load.engine.GlideException:获取数据失败,类java.io.InputStream,LOCAL 原因(1 of 1):类java.io.FileNotFoundException:没有这样的文件或目录 原因(2 of 2):class com.bumptech.glide.load.engine.GlideException:获取数据失败,类android.os.ParcelFileDescriptor,LOCAL 原因(1 of 1):类java.io.FileNotFoundException:没有这样的文件或目录
代码在片段
中private File output;
private static final String EXTRA_FILENAME=
"com.tsiro.dogvip.EXTRA_FILENAME";
private static final String FILENAME="cameraimage.jpeg";
private static final String PHOTOS="photos";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mView == null) {
mBinding = DataBindingUtil.inflate(inflater, R.layout.owner_frgmt, container, false);
mView = mBinding.getRoot();
}
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mCommonUtls = new CommonUtls(getActivity());
mOwnerFrgmtViewModel = new OwnerViewModel(MyPetsRequestManager.getInstance());
if (savedInstanceState != null) {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
}
private void initializePickProfilePhotoDialog() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
output=new File(new File(getActivity().getFilesDir(), PHOTOS), timeStamp);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", output);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip = ClipData.newUri(getActivity().getContentResolver(), "A photo", outputUri);
intent.setClipData(clip);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList= getActivity().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getActivity().grantUriPermission(packageName, outputUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
Log.e(debugTag, intent.getData()+"");
startActivityForResult(intent, AppConfig.ACTION_IMAGE_CAPTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppConfig.EXTERNAL_CONTENT_URI || requestCode == AppConfig.ACTION_IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Uri uri;
boolean cameraImg = false;
if (requestCode == AppConfig.ACTION_IMAGE_CAPTURE) {
uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", output);
cameraImg = true;
} else {
uri = data.getData();
}
if (mCommonUtls.isImageSizeValid(uri, cameraImg, output)) {
setUserProfileImg(uri);
} else {
((MyPetsActivity)getActivity()).showSnackBar(R.style.SnackBarSingleLine, getResources().getString(R.string.invalid_image_size));
}
}
}
}
private void setUserProfileImg(final Uri uri) {
if (uri != null) {
Glide.with(getActivity())
.load(uri)
.transition(withCrossFade())
.apply(new RequestOptions()
.circleCrop())
.into(mBinding.profileImgv);
}
}
如果有人能帮助我......那很紧急。谢谢。