棉花糖设备中的图像保存问题

时间:2016-02-03 07:21:45

标签: android android-camera android-6.0-marshmallow

目标:我的应用程序从相机和图库中拍摄图像。裁剪此图像并保存到外部存储。

问题:应用程序显示不幸的是,当您从裁剪页面点击保存按钮时,照片会显示错误信息。此错误仅在棉花糖设备中发生。在所有其他设备中正常工作。

我拍摄图像的代码如下:

final CharSequence[] options = {"Take Photo", "Choose from Gallery"};
            AlertDialog.Builder builder = new AlertDialog.Builder(CategoryActivity.this);
            builder.setTitle("Select Pic Using...");
            builder.setItems(options, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (options[item].equals("Take Photo")) {

                        try {
                            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                            pic = new File(Environment.getExternalStorageDirectory(),
                                    "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");

                            picUri = Uri.fromFile(pic);

                            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, picUri);

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

                            startActivityForResult(cameraIntent, LOAD_IMAGE_CAMERA);
                        } catch (ActivityNotFoundException e) {
                            e.printStackTrace();
                        }

                    } else if (options[item].equals("Choose from Gallery")) {
                        Intent intent = new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);


                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), LOAD_IMAGE_GALLARY);
                        SharedPreferences.Editor editor = pref.edit();
                        editor.putBoolean("profile", false);
                        editor.commit();
                        //  finish();
                    }
                }
            });

            builder.show();
        }
    });

if (requestCode == LOAD_IMAGE_CAMERA && resultCode == RESULT_OK) {

        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("profile", false);
        editor.commit();

        Intent in = new Intent(CategoryActivity.this, ImageCrop.class);

        in.putExtra("URI", picUri);
        in.putExtra("cat", type);
        in.putExtra("contest_idadd", contestid_i);


        startActivity(in);


    } else if (requestCode == LOAD_IMAGE_GALLARY) {
        if (data != null) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("profile", false);
            editor.commit();

            picUri = data.getData();

            Intent in = new Intent(CategoryActivity.this, ImageCrop.class);
            in.putExtra("URI", picUri);
            in.putExtra("cat", type);
            in.putExtra("contest_idadd", contestid_i);

            startActivity(in);

        }
    } 

使用以下代码裁剪

        Bundle bundle = getIntent().getExtras();

            if (bundle != null) {
                picUri = (Uri) bundle.get("URI");
                cat_value = (String) bundle.get("cat");
            }
    protected void CropImage() {
            try {
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(picUri, "image/*");
    Log.d("piccccc",picUri+"");
                intent.putExtra("crop", "true");
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("aspectX", 3);
                intent.putExtra("aspectY", 3);
                intent.putExtra("scaleUpIfNeeded", true);
                intent.putExtra("return-data", true);

                startActivityForResult(intent, CROP_IMAGE);

            } catch (ActivityNotFoundException e) {

            }
        }

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

            if (requestCode == CROP_IMAGE) {
                if (data != null) {
                    // get the returned data

                    Bundle extras = data.getExtras();

                    // get the cropped bitmap
                    Bitmap photo = extras.getParcelable("data");

                    image_array.add(photo);

                    crop.setImageBitmap(photo);

                    if (pic != null) {

                        if (pic.delete()) {

                        }
            }
                }
                else
                {
                    Log.d("cropppppppppppppp", requestCode + "");
                }
            }

在marshmallow设备中,在保存的外部存储路径中找不到图像。任何人请给我一个解决方案。

2 个答案:

答案 0 :(得分:1)

添加适当的执行实施 android.permission.WRITE_EXTERNAL_STORAGE 您的代码中的权限。 您需要处理来自marshmallow及以上代码的权限:

http://developer.android.com/training/permissions/index.html

http://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:1)

使用SDK 22或更低版本构建您的应用,TargetSdkVersion = 22