Android 7.0 调用相机 android.os.FileUriExposedException

import android.R.integer;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.comsydactivity.utils.SharedPreferenceUtil;





/**
 * 自定义Dialog
 * 
 */
public class BottomDialog2 extends Dialog {
   private String TAG = "BottomDialog";
   private String IMAGE_FILE_LOCATION;
   private int START_PAIZHAO;
   /** 手机相册 */
   private int START_PHONEIMG;
   private Context context;
   private static final String TYPE_OF_GET_CONTENT = "image/*";
   private int flag;
   private int code;
   private String fileNameString = Environment.getExternalStorageDirectory() + "/up_photos/";
   //public static String filenamelast;
   public BottomDialog2(Activity ctx, int layout, int width, int height) {
      this(ctx.getLayoutInflater().inflate(layout, null), width, height);
   }

   public BottomDialog2(View view, int width, int height) {
      this(view, width, height, R.style.BottomDialog);
   }

   public BottomDialog2(View view, int width, int height, Context context,
          int START_PAIZHAO, int START_PHONEIMG) {
      this(view, width, height, R.style.BottomDialog);
      this.context = context;
      this.START_PAIZHAO = START_PAIZHAO;
      this.START_PHONEIMG = START_PHONEIMG;
   }

   public BottomDialog2(View view, int width, int height, int style) {
      super(view.getContext(), style);

      // // 透明背景
      // Drawable myDrawable =
      // context.getResources().getDrawable(R.drawable.dialog_root_bg);
      // myDrawable.setAlpha(150);
      // view.setBackgroundDrawable(myDrawable);
      setContentView(view);

      Window window = getWindow();
      WindowManager.LayoutParams params = window.getAttributes();
      params.width = width;
      params.height = height;
      // params.height = (int) (default_height * density);
      // view.measure(0, 0);
      // params.width = view.getMeasuredWidth();
      // params.height = view.getMeasuredHeight();
      params.gravity = Gravity.CENTER;
      window.setAttributes(params);

      // 此处可以设置dialog显示的位置
      window.setGravity(Gravity.BOTTOM);
      // 添加动画
      // window.setWindowAnimations(R.style.popup_in_out);
      setOnClickListener(R.id.btn_paishe, new View.OnClickListener() {

         @Override
         public void onClick(View arg0) {

            if (Environment.getExternalStorageState().equals(
                  Environment.MEDIA_MOUNTED)) {
//             Intent intent = new Intent(
//                   MediaStore.ACTION_IMAGE_CAPTURE);
//              File out = new File(getPhotoPath());
//                        Uri uri = Uri.fromFile(out);
//                        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//             intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//             ((Activity) context).startActivityForResult(intent, 2);

               //适配7.0
               Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               Uri uri;
               File out = new File(getPhotoPath());
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系统
                  ContentValues contentValues = new ContentValues(1);
                  contentValues.put(MediaStore.Images.Media.DATA, getPhotoPath());
                  uri= context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
               }else{

                  uri = Uri.fromFile(out);
               }
               intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
               ((Activity) context).startActivityForResult(intent, 2);

               dismiss();
            } else {
               Toast.makeText(context, "你的手机没有SD卡,不支持该功能!",
                     Toast.LENGTH_SHORT).show();
               dismiss();
            }

         }
      });
      setOnClickListener(R.id.btn_xuanpic, new View.OnClickListener() {

         @Override
         public void onClick(View arg0) {

               if (Environment.getExternalStorageState().equals(
                     Environment.MEDIA_MOUNTED)) {
                  // Intent intent = new Intent(
                  // Intent.ACTION_PICK,
                  // MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  // ((Activity)context).startActivityForResult(intent,
                  // START_PHONEIMG);
                  Intent intent = new Intent(Intent.ACTION_PICK);
                  intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, TYPE_OF_GET_CONTENT);
                  ((Activity) context).startActivityForResult(intent, 1);
                  dismiss();
               } else {
                  Toast.makeText(context, "你的手机没有SD卡,不支持该功能!",
                        Toast.LENGTH_SHORT).show();
                  dismiss();
               }

         }
      });
      setOnClickListener(R.id.btn_cancel, new View.OnClickListener() {
         @Override
         public void onClick(View arg0) {
            dismiss();
         }
      });
   }

   /**
    * 为指定id控件设置点击监听
    * 
    * @param id
    * @param listener
    */
   public BottomDialog2 setOnClickListener(int id, View.OnClickListener listener) {
      View view = findViewById(id);
      if (view != null && listener != null)
         view.setOnClickListener(listener);
      return this;
   }

   public void setOutsideTouchable(boolean touchable) {
   }

   public void setBackgroundDrawable(Drawable background) {
   }

   public void setAnimationStyle(int animationStyle) {
   }

   // public void setOnDismissListener(final
   // android.widget.PopupWindow.OnDismissListener onDismissListener){
   // this.setOnDismissListener(new OnDismissListener() {
   // @Override
   // public void onDismiss(DialogInterface dialog) {
   // onDismissListener.onDismiss();
   // }
   // });
   // }
   public void showAtLocation(View parent, int gravity, int x, int y) {
      show();
   }
   
   
   /**
     * 获得原图片保存路径
     * @return
     */
    public String getPhotoPath()
    {
       SharedPreferenceUtil.getInstance().savePhonePath("");
       String filename = "";
       // 获取 SD 卡根目录
        String saveDir = fileNameString;
        // 新建目录
        File dir = new File(saveDir);
        if (!dir.exists())
        {
            dir.mkdir();
        }
        // 生成文件名
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        filename = saveDir + "MT" + (time.format(new Date())) + ".jpg";
       //filenamelast = filename;
       
       // 为确保文件名有值,保存到本地
       SharedPreferenceUtil.getInstance().savePhonePath(filename);
       
       return filename;
    }

}

 

 

// 调用

 

/**
 * 拍照
 */
private static final int START_PAIZHAO = 111;
/**
 * 相册
 */
private static final int START_PHONEIMG = 222;
View view = getLayoutInflater().inflate(R.layout.bottom_item_dialog,
      null);
bd = new BottomDialog2(view, ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT, RepayCaiLiaoImageActivity.this,
      START_PAIZHAO, START_PHONEIMG);
bd.show();

你可能感兴趣的:(Android 7.0 调用相机 android.os.FileUriExposedException)