安卓7.0拍照问题

搞了好几天。一直都拍不了照,自动退出,不然就是拍了显示不出来,最后还是看书成功的。简单总结了一下,下面有从网上搜的各种代码,都尝试过了,最后的是书本的。

///--------能拍照,拍了显示不了----------
                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        //获取系統版本
                        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                        if (currentapiVersion < 24) {
                            // 从文件中创建uri
                            imageUri = Uri.fromFile(imgfile);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        } else {
                            //兼容android7.0 使用共享文件的形式
                            ContentValues contentValues = new ContentValues(1);
                            contentValues.put(MediaStore.Images.Media.DATA, imgfile.getAbsolutePath());
                            //检查是否有存储权限,以免崩溃
                            if (ContextCompat.checkSelfPermission(AddinfoActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                    != PackageManager.PERMISSION_GRANTED) {
                                //申请WRITE_EXTERNAL_STORAGE权限
                                Toast.makeText(AddinfoActivity.this, "请开启存储权限", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        }
                        Log.d(TAG, "onClick2:"+imageUri);
                        tmp_path = imgfile.getPath();
                        Log.d(TAG, "onClick3:"+tmp_path);
                        // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
                        startActivityForResult(intent, 11);
//拍照功能
                Time time = new Time();
                time.setToNow();
                String strtime = time.year+(time.month+1)+time.monthDay+time.hour+time.minute+time.second+""; //加个空将数字格式转为字符串格式
                tmp_path = Environment.getExternalStorageState()+"/image"+strtime+".jpg";
                //Log.d(TAG, "onClick2:"+tmp_path);
                File imgfile = new File(tmp_path);
                Log.d(TAG, "onClick3:"+imgfile);
                try {
                    if(imgfile.exists()){
                        imgfile.delete();
                    }
                    imgfile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                /*Uri imageUri;
                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                     imageUri = FileProvider.getUriForFile(AddinfoActivity.this, "com.example.a17251101128_chen_planner.fileprovider", imgfile);
                }
                else {
                     imageUri = Uri.fromFile(imgfile);
                }
                Log.d(TAG, "onClick4: "+ imageUri);*/
                //Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                //intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                Uri imageUri;
                if (ContextCompat.checkSelfPermission(AddinfoActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions((Activity) AddinfoActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                } else {
                    //权限已经被授予,在这里直接写要执行的相应方法即可
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        imageUri = FileProvider.getUriForFile(AddinfoActivity.this, "com.example.a17251101128_chen_planner.fileprovider", imgfile);
                    }
                    else {
                        imageUri = Uri.fromFile(imgfile);
                    }
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    // 指定调用相机拍照后照片的储存路径
                    //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgfile));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, 11);
                }
  1. 在上面的基础上小改
//currentapiVersion的方法,这个可以拍照的
                int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                Log.e("currentapiVersion","currentapiVersion====>"+currentapiVersion);
                if (currentapiVersion<24){
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgfile));
                    startActivityForResult(intent, 11);
                }else {
                    ContentValues contentValues = new ContentValues(1);
                    contentValues.put(MediaStore.Images.Media.DATA, imgfile.getAbsolutePath());
                    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, 11);
                }

最终我的版本

//拍照功能
                      Time time = new Time();
                        time.setToNow();
                        String strtime = time.year+(time.month+1)+time.monthDay+time.hour+time.minute+time.second+""; //加个空将数字格式转为字符串格式
                        //tmp_path = Environment.getExternalStorageState()+"/image"+strtime+".jpg";   //原来是这里的问题

                        tmp_path = getExternalCacheDir()+"/image"+strtime+".jpg";  //这里  
                        //File imgfile = new File(getExternalCacheDir(), tmp_path);
                        File imgfile = new File(tmp_path);
                        try {
                            if(imgfile.exists()){
                                imgfile.delete();
                            }
                            imgfile.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        ///--------拍照功能,拍了显示不了,可以了现在----------
                        if (ContextCompat.checkSelfPermission(AddinfoActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions((Activity) AddinfoActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                        } else {
                            //权限已经被授予,在这里直接写要执行的相应方法即可
                            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                imageUri = FileProvider.getUriForFile(AddinfoActivity.this, "com.example.a17251101128_chen_planner.fileprovider", imgfile);
                            }
                            else {
                                imageUri = Uri.fromFile(imgfile);
                            }
                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                            // 指定调用相机拍照后照片的储存路径
                            //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgfile));
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                            startActivityForResult(intent, 11);
                        }

你可能感兴趣的:(安卓7.0拍照问题)