Android N以上出现FileUriExposedException错误

报错信息

 android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/634image.jpg exposed beyond app through ClipData.Item.getUri()
                                                       at android.os.StrictMode.onFileUriExposed(StrictMode.java:1816)
                                                       at android.net.Uri.checkFileUriExposed(Uri.java:2350)
                                                       at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
                                                       at android.content.Intent.prepareToLeaveProcess(Intent.java:9054)
                                                       at android.content.Intent.prepareToLeaveProcess(Intent.java:9039)
                                                       at android.app.Instrumentation.execStartActivity(Instrumentation.java:1530)
                                                       at android.app.Activity.startActivityForResult(Activity.java:4391)
                                                       at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                       at android.app.Activity.startActivityForResult(Activity.java:4335)
                                                       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                       at com.jiuhong.boyuan.utlis.ImageUtlis.gotoCa(ImageUtlis.java:75)
                                                       at com.jiuhong.boyuan.fragment.MyHomeFragment$3$1.onClick(MyHomeFragment.java:183)
                                                       at android.view.View.performClick(View.java:5646)
                                                       at android.view.View$PerformClick.run(View.java:22459)
                                                       at android.os.Handler.handleCallback(Handler.java:761)
                                                       at android.os.Handler.dispatchMessage(Handler.java:98)
                                                       at android.os.Looper.loop(Looper.java:156)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

以前我们使用都是Uri.fromFile(路劲)

现在使用 :

   ContentValues contentValues = new ContentValues(1);
                contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                Uri uri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

网上的那种xml试过但不是很好用,建议试试这种

记得使用的时候加上版本判断,就像这样

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


                ContentValues contentValues = new ContentValues(1);
                contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                Uri uri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

                intentFromCapture.putExtra(
                        MediaStore.EXTRA_OUTPUT,
                        uri);
            } else {
                intentFromCapture.putExtra(
                        MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(file));
            }

OK!大功告成

但是拍照加剪裁的时候会出现

SQLiteDatabase: Error inserting storage_id=65537 media_type=1 mime_type=image/jpeg title=1502763712406 _data=/storage/emulated/0/Pictures/1502763712406.jpg bucket_id=-1617409521 android.intent.extra.videoQuality=/storage/emulated/0/DCIM/591image.xy date_added=1502763712 is_hw_burst=0 bucket_display_name=Pictures _display_name=1502763712406.jpg parent=6 format=14337
                                                 android.database.sqlite.SQLiteException: near ".": syntax error (Sqlite code 1): , while compiling: INSERT INTO files(storage_id,media_type,mime_type,title,_data,bucket_id,android.intent.extra.videoQuality,date_added,is_hw_burst,bucket_display_name,_display_name,parent,format) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?), (OS error - 2:No such file or directory)
                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:910)
                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:521)
                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)
                                                     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:63)
                                                     at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
                                                     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1725)
                                                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1593)
                                                     at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:3726)
                                                     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:4152)
                                                     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:3342)
                                                     at android.content.ContentProvider$Transport.insert(ContentProvider.java:264)
                                                     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
                                                     at android.os.Binder.execTransact(Binder.java:565)

会各有不一但本质相同(提示数据库插入错误 0.0 才发现ID不对,脑子一抽说改ID,才发现是底层做的.....这还改毛啊)
解决办法:(转换uri留着备用)

 /**
     * 转换 content:// uri
     *
     * @param imageFile
     * @return
     */
    public Uri getImageContentUri(File imageFile, Context context) {
        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;
            }
        }
    }

剪裁照片的时候用这个

private void crop(String imagePath) {
    File file = new File("xxx.jpg");
    cropImagePath = file.getAbsolutePath();

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", config.aspectX);//config.aspectX ->填写你自己的数字
    intent.putExtra("aspectY", config.aspectY);//填写你自己的数字
    intent.putExtra("outputX", config.outputX);//填写你自己的数字
    intent.putExtra("outputY", config.outputY)//;填写你自己的数字
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    startActivityForResult(intent, IMAGE_CROP_CODE);
}

使用的时候根据判断方法就OK了

相册工具类全部

package com.jiuhong.boyuan.utlis;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.v4.BuildConfig;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.Toast;

import java.io.File;


import static android.app.Activity.RESULT_CANCELED;

/**
 * Created by Administrator on 2017/7/3.
 */

/*
*
*   图片工具类
* */
public class ImageUtlis {
    /**
     * 图片名称
     */
    private static final String IMAGE_FILE_NAME = "image.jpg";
    /**
     * 使用照相机拍照获取图片
     */
    public static final int SELECT_PIC_BY_TACK_PHOTO = 0;
    /**
     * 使用相册中的图片
     */
    public static final int SELECT_PIC_BY_PICK_PHOTO = 1;

    private static final int RESULT_REQUEST_CODE = 2;
    private static File path;
    private static IImageBitmap iImageBitmap1;
    private static File tempFile;

    private static Activity activity1;

    //跳转到相册
    public static void gotoImage(Activity activity) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        activity.startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
        activity1 = activity;
    }

    //相机获取
    public static void gotoCa(Activity activity) {
        activity1 = activity;
        Intent intentFromCapture = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        // 判断存储卡是否可以用,可用进行存储
        String state = Environment
                .getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            path = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            if (!path.exists())
                path.mkdirs();
            File file = new File(path, IMAGE_FILE_NAME);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intentFromCapture.putExtra(
                        MediaStore.EXTRA_OUTPUT,
                        getImageContentUri(file, activity));

            } else {
                intentFromCapture.putExtra(
                        MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(file));
            }


        }

        activity.startActivityForResult(intentFromCapture,
                SELECT_PIC_BY_TACK_PHOTO);

    }

    //回调

    public static void intentGet(int requestCode, int resultCode, Intent data, Activity activity) {


        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                //相册传递过来的
                case SELECT_PIC_BY_PICK_PHOTO:
                    startPhotoZoom(data.getData());
                    break;
                //相机传递过来的
                case SELECT_PIC_BY_TACK_PHOTO:
                    // 判断存储卡是否可以用,可用进行存储
                    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                    tempFile = new File(path, IMAGE_FILE_NAME);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        crop(activity);
                    } else {
                        startPhotoZoom(Uri.fromFile(tempFile));
                    }

                    break;
                case RESULT_REQUEST_CODE: // 图片缩放完成后
                    Log.e("?????????", "intentGet: " + "wozoule ?????");
                    Log.e("图片", "intentGet: " + data.getExtras().getParcelable("data"));
                    if (data != null) {
                        getImageToView(data);
                    } else {
                        Log.i("hgfgdsdefsgr", "onActivityResult: " + data);
                    }
                    break;
            }

        }
    }

    /**
     * 转换 content:// uri
     *
     * @param imageFile
     * @return
     */
    public static Uri getImageContentUri(File imageFile, Context context) {
        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;
            }
        }
    }

    //创建回调
    public static void setOnBitMapListener(IImageBitmap iImageBitmap) {
        iImageBitmap1 = iImageBitmap;
    }

    //创建回调


    public interface IImageBitmap {

        void getBitMap(boolean isXC, Bitmap bitmap);

    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public static void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 120);
        intent.putExtra("outputY", 120);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        activity1.startActivityForResult(intent, RESULT_REQUEST_CODE);
        activity1 = null;

        Log.e("----wozoule ???", "startPhotoZoom: " + "我走了???????");
    }

    private static void crop(Context context) {
        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = getImageContentUri(tempFile, context);
        } else {
            uri = Uri.fromFile(tempFile);
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 120);
        intent.putExtra("outputY", 120);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        activity1.startActivityForResult(intent, RESULT_REQUEST_CODE);
    }

    /**
     * 保存裁剪之后的图片数据
     * 

* // * @param picdata */ private static void getImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); if (iImageBitmap1 != null) iImageBitmap1.getBitMap(true, photo); } } }

我比较懒,所以数字那块就直接写死了,,哈哈哈哈哈哈

第二版..可能第一版比较乱....

在AndroidManifest中加入

  
            
        
  >

xml/rc_file_path的显示内容

   

JAVA代码

//如果版本大于7.0就使用最新的
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //uri = getImageContentUri(tempFile, context);
            //注意"你的包名.FileProvide"r要和你XML的保持一致
            uri = FileProvider.getUriForFile(context, "你的包名.FileProvider", tempFile);
        } else {
//普通的就用之前的
            uri = Uri.fromFile(tempFile);
        }

现在就OK了

你可能感兴趣的:(Android N以上出现FileUriExposedException错误)