拍照及解决7.0相机打不开问题(文件曝光率异常)

private Bitmaphead;// 头像Bitmap

private static Stringpath ="/sdcard/myHead/";


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my_message);

ButterKnife.bind(this);

Bitmap bt = BitmapFactory.decodeFile(path +"head.jpg");// 从SD卡中找头像,转换成Bitmap

    if (bt !=null) {

@SuppressWarnings("deprecation")

Drawable drawable =new BitmapDrawable(bt);// 转换成drawable

        userIcon.setImageDrawable(drawable);

}

}


创建MyApp onCreat()中可解决文件曝光率异常

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

StrictMode.VmPolicy.Builder builder =new StrictMode.VmPolicy.Builder();

StrictMode.setVmPolicy(builder.build());

}


点击事件时调用

private void initpop() {

View view = LayoutInflater.from(this).inflate(R.layout.pop,null);

final PopupWindow popupWindow =new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);

popupWindow.setContentView(view);

popupWindow.setOutsideTouchable(true);

popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

popupWindow.showAtLocation(view, Gravity.CENTER,0,0);

popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

@Override

            public void onDismiss() {

bgAlpha(1f);

}

});

View ta = view.findViewById(R.id.take_photo);

View be = view.findViewById(R.id.bendi);

ta.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View view) {

Intent intent2 =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri photoURI = FileProvider.getUriForFile

                        (MyMessageActivity.this, getApplicationContext().getPackageName() +".provider",new File(Environment.getExternalStorageDirectory(),"head.jpg"));

intent2.putExtra(MediaStore.EXTRA_OUTPUT,

photoURI);

//                intent2.putExtra(MediaStore.EXTRA_OUTPUT,

//                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));

                startActivityForResult(intent2,2);// 采用ForResult打开

                popupWindow.dismiss();

}

});

be.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View view) {

Intent intent1 =new Intent(Intent.ACTION_PICK,null);

//打开文件

                intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");

startActivityForResult(intent1,1);

popupWindow.dismiss();

}

});

}


@Override

protected void onActivityResult(int requestCode,int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case 1:

if (resultCode ==RESULT_OK) {

cropPhoto(data.getData());// 裁剪图片

            }

break;

case 2:

if (resultCode ==RESULT_OK) {

File temp =new File(Environment.getExternalStorageDirectory() +"/head.jpg");

cropPhoto(Uri.fromFile(temp));// 裁剪图片

            }

break;

case 3:

if (data !=null) {

Bundle extras = data.getExtras();

head = extras.getParcelable("data");

if (head !=null) {

/**

                    * 上传服务器代码

                    */

                    setPicToView(head);// 保存在SD卡中

                    userIcon.setImageBitmap(head);// 用ImageButton显示出来

                }

}

break;

default:

break;

}

}

/**

* 调用系统的裁剪功能

*

* @param uri

*/

public void cropPhoto(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",250);

intent.putExtra("outputY",250);

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

startActivityForResult(intent,3);

}

private void setPicToView(Bitmap mBitmap) {

String sdStatus = Environment.getExternalStorageState();

if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {// 检测sd是否可用

        return;

}

FileOutputStream b =null;

File file =new File(path);

file.mkdirs();// 创建文件夹

    String fileName =path +"head.jpg";// 图片名字

    try {

b =new FileOutputStream(fileName);

mBitmap.compress(Bitmap.CompressFormat.JPEG,100, b);// 把数据写入文件

    }catch (FileNotFoundException e) {

e.printStackTrace();

}finally {

try {

// 关闭流

            b.flush();

b.close();

}catch (IOException e) {

e.printStackTrace();

           }

    }

}

你可能感兴趣的:(拍照及解决7.0相机打不开问题(文件曝光率异常))