1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 小米相册无法获取 适配小米华为手机等拍照后获取不到照片

android 小米相册无法获取 适配小米华为手机等拍照后获取不到照片

时间:2022-02-27 16:32:42

相关推荐

android 小米相册无法获取 适配小米华为手机等拍照后获取不到照片

问题摘要:适配小米华为手机等拍照后获取不到照片

出现场景

普通的相机调用,在 intent 传进去一个路径,然调用这个意图。

在测试机 荣耀 8x 上是没有问题的,能获取到拍的照片。

在小米系统和 华为麦芒4上就不行,路径上就没有照片。

/**

* @param file 拍照生成的照片地址

* @return intent

*/

public static intent gettakepictureintent(file file) {

intent intent = new intent(mediastore.action_image_capture);

if (intent.resolveactivity(commonutils.context.getpackagemanager()) != null) {

if (null != file) {

temppicturepath = file.getpath();

logutil.log(dautils.class.getsimplename(),"gettakepictureintent :->>"+temppicturepath);

if (build.version.sdk_int < build.version_codes.n) {

intent.putextra(mediastore.extra_output, uri.fromfile(file));

} else {

contentvalues contentvalues = new contentvalues(1);

contentvalues.put(mediastore.images.media.data, file.getabsolutepath());

// uri uri = commonutils.context.getcontentresolver().insert(mediastore.images.media.external_content_uri, contentvalues);

uri uri = fileprovider.geturiforfile(commonutils.context, "xxx.fileprovider", file.getabsolutefile());

intent.putextra(mediastore.extra_output, uri);

}

}

}

return intent;

}

出现原因

不能获取到照片的原因是因为这个照片的目录没有创建。

在传入 uri 之前要把照片的目录给创建出来。不然就拿不到照片。

如何修复

修改如下:在传入照片 uri 前保证目录已经被创建

/**

* @param file 拍照生成的照片地址

* @return intent

*/

public static intent gettakepictureintent(file file) {

intent intent = new intent(mediastore.action_image_capture);

if (intent.resolveactivity(commonutils.context.getpackagemanager()) != null) {

if (null != file) {

if (!file.getparentfile().exists()){

file.getparentfile().mkdirs();

}

temppicturepath = file.getpath();

logutil.log(dautils.class.getsimplename(),"gettakepictureintent :->>"+temppicturepath);

if (build.version.sdk_int < build.version_codes.n) {

intent.putextra(mediastore.extra_output, uri.fromfile(file));

} else {

contentvalues contentvalues = new contentvalues(1);

contentvalues.put(mediastore.images.media.data, file.getabsolutepath());

// uri uri = commonutils.context.getcontentresolver().insert(mediastore.images.media.external_content_uri, contentvalues);

uri uri = fileprovider.geturiforfile(commonutils.context, "xxx.fileprovider", file.getabsolutefile());

intent.putextra(mediastore.extra_output, uri);

}

}

}

return intent;

}

排查过程

反复 debug 断点,google 半小时无果后,灵光一闪,想到了。

总结

这是个坑

end

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。