美文网首页
Android7.0 、8.0拍照、相册选取照片裁剪照片时闪退问

Android7.0 、8.0拍照、相册选取照片裁剪照片时闪退问

作者: 小草_c452 | 来源:发表于2019-01-19 14:48 被阅读0次

Android 7.0、8.0以后的手机,打开手机拍照或者打开相册时应用会闪退产生错误日志:


android.os.FileUriExposedException: ** exposed beyond app through Intent.getData()


解决的方式:在AndroidManifast.xml中添加如下代码:(具体代码文末付上)

其中的file_paths需要用户在res下新建文件夹xml创建file_paths.xml文件代码如下:(具体代码文末付上)

注意:红色部分为自己的包名

在拍照时注意进行判断sdk版本号即当前手机的版本进行以下设置:



这样的话使用系统自带的拍照和选择相册功能就不会出现闪退和功能了。

但是有的同学在拍照和选择照片以后还使用了系统带的裁剪功能,这样也会出现闪退,具体的日志就不付了,这是由于Android7.0以后将uri(file)功能转变了所产生导致的,修复在这个功能只需要一下设置:


方法cropPhoto中uri参数需要这样设置才行:

这样到此就ok了;附上相关代码:


    android:name="android.support.v4.content.FileProvider"

    android:authorities="com.ddzn.suyan.fileProvider"

    android:exported="false"

    android:grantUriPermissions="true">

        android:name="android.support.FILE_PROVIDER_PATHS"

        android:resource="@xml/file_paths" />


<?xml version="1.0" encoding="utf-8"?>

    <external-path path="Android/data/com/ddzn/suyan/" name="files_root" />

    <external-path path="." name="external_storage_root" />

    <external-path name="external_files" path="."/>

</paths>


public void cameraPicker() {

this.imageFile =new File(Settings.TEMP_PATH, UUID.randomUUID() +".jpg");

    Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (this.imageFile !=null) {

Uri imageUri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

imageUri = FileProvider.getUriForFile(activity.get(), activity.get().getPackageName() +".fileProvider", imageFile);

        }else {

imageUri = Uri.fromFile(imageFile);

        }

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // set the image file name

    }

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    this.activity.get().startActivityForResult(intent, Constants.REQUEST_CODE_CAMERA);

}



/**

* @param uri

* @param width  裁剪后的宽度

* @param height 裁剪后的高度

*/

public void cropPhoto(Uri uri, int width, int height) {

cropFileName = UUID.randomUUID() +".jpg";

    Uri imageUri = Uri.fromFile(new File(Settings.TEMP_PATH, cropFileName));

    Intent intent =new Intent("com.android.camera.action.CROP");

    intent.setDataAndType(uri, "image/*");

    intent.putExtra("crop", "true");

    // aspectX aspectY 是宽高的比例

    intent.putExtra("aspectX", width);

    intent.putExtra("aspectY", height);

    // outputX outputY 是裁剪图片宽高

    intent.putExtra("scale", true);// 黑边

    intent.putExtra("scaleUpIfNeeded", true);// 黑边

    intent.putExtra("outputX", width);

    intent.putExtra("outputY", height);

    intent.putExtra("return-data", false);

    intent.putExtra("output", imageUri); // 转入目标文件

    this.activity.get().startActivityForResult(intent, Constants.REQUEST_CODE_CROP);

}



public static UrigetImageContentUri(Context context, File imageFile) {

String filePath = imageFile.getAbsolutePath();

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID},

            MediaStore.Images.Media.DATA +"=? ", new String[]{filePath}, null);

    if (cursor !=null && cursor.moveToFirst()) {

int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));

        Uri baseUri = Uri.parse("content://media/external/images/media");

        return Uri.withAppendedPath(baseUri, "" + id);

    }else {

if (imageFile.exists()) {

ContentValues values =new ContentValues();

            values.put(MediaStore.Images.Media.DATA, filePath);

            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        }else {

return null;

        }

}

}

相关文章

网友评论

      本文标题:Android7.0 、8.0拍照、相册选取照片裁剪照片时闪退问

      本文链接:https://www.haomeiwen.com/subject/bzxzdqtx.html