简体   繁体   English

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

[英]Image saving issue in marshmallow devices

Aim :my application take images from camera and gallery.And crop this image and save to external storage. 目的:我的应用程序从相机和图库中获取图像,并将其裁剪并保存到外部存储中。

Issue : Application shows unfortunately photos has stooped message while clicking save button from crop page.The error happens only in marshmallow devices.working fine in all other devices. 问题:应用程序显示不幸的是,在裁剪页面上单击保存按钮时,照片显示弯弯的消息。此错误仅在棉花糖设备上发生。在所有其他设备上均能正常工作。

my code to take image is given below: 我的拍照代码如下:

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);

        }
    } 

And crop using the below code 并使用以下代码裁剪

        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 + "");
                }
            }

In marshmallow devices images not found in saved external storage path.Anyone please give me a solution. 在棉花糖设备中,在保存的外部存储路径中找不到图像。任何人请给我一个解决方案。

Add proper implementation of taking android.permission.WRITE_EXTERNAL_STORAGE permission in your code. 在代码中添加获得android.permission.WRITE_EXTERNAL_STORAGE权限的正确实现。 You need to handle permissions in your code from marshmallow and above: 您需要从棉花糖及更高版本处理代码中的权限:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM