欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 7.0调用相机崩溃详解及解决办法

程序员文章站 2024-02-27 19:22:33
android 7.0调用相机崩溃解决办法  错误提示: android.os.fileuriexposedexception: file:///st...

android 7.0调用相机崩溃解决办法

 错误提示:

android.os.fileuriexposedexception: file:///storage/emulated/0/dcim/img_1041503431.jpg exposed beyond app through clipdata.item.geturi()

Android 7.0调用相机崩溃详解及解决办法

处理方式

/**
 * open camera
 */
private void showcameraaction() {
 if (contextcompat.checkselfpermission(getcontext(), manifest.permission.write_external_storage)
   != packagemanager.permission_granted) {
  requestpermission(manifest.permission.write_external_storage,
    getstring(r.string.mis_permission_rationale_write_storage),
    request_storage_write_access_permission);
 } else {
  intent intent = new intent(mediastore.action_image_capture);
  if (intent.resolveactivity(getactivity().getpackagemanager()) != null) {
   try {
    mtmpfile = fileutils.createtmpfile(getactivity());
   } catch (ioexception e) {
    e.printstacktrace();
   }
   if (mtmpfile != null && mtmpfile.exists()) {
     /*获取当前系统的android版本号*/
    int currentapiversion = android.os.build.version.sdk_int;
    log.e("currentapiversion","currentapiversion====>"+currentapiversion);
    if (currentapiversion<24){
     intent.putextra(mediastore.extra_output, uri.fromfile(mtmpfile));
     startactivityforresult(intent, request_camera);
    }else {
     contentvalues contentvalues = new contentvalues(1);
     contentvalues.put(mediastore.images.media.data, mtmpfile.getabsolutepath());
     uri uri = getcontext().getcontentresolver().insert(mediastore.images.media.external_content_uri,contentvalues);
     intent.putextra(mediastore.extra_output, uri);
     startactivityforresult(intent, request_camera);
    }
   } else {
    toast.maketext(getactivity(), r.string.mis_error_image_not_exist, toast.length_short).show();
   }
  } else {
   toast.maketext(getactivity(), r.string.mis_msg_no_camera, toast.length_short).show();
  }
 }
}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!