0

我正在尝试从相机捕获图像。我有一个代码,除了 android marshmallow 设备之外的所有设备都可以正常工作。它将结果返回到画廊最近的文件夹。我已经检查了很多次。我不知道这是怎么回事,请帮忙。

 public void getImage() {

    final PermissionManager permissionManager = new PermissionManager();


    if (permissionManager.userHasPermission(Addfood_items.this.getContext())) {


        if (Build.VERSION.SDK_INT > M) {

            GetImageFromGallery();


        } else {

            oldGetImageFromGallery();


        }


    } else {
        permissionManager.requestPermission(getActivity());
    }




}


public void oldImageCropFunction() {

    // Image Crop Code
    try {


        CropIntent = new Intent("com.android.camera.action.CROP");

        CropIntent.setDataAndType(uri, "image/*");

        CropIntent.putExtra("crop", "true");
        CropIntent.putExtra("outputX", 300);
        CropIntent.putExtra("outputY", 300);
        CropIntent.putExtra("aspectX", 3);
        CropIntent.putExtra("aspectY", 3);
        CropIntent.putExtra("scaleUpIfNeeded", true);
        CropIntent.putExtra("return-data", true);

        startActivityForResult(CropIntent, 1);

    } catch (ActivityNotFoundException e) {

    }
}


public void ImageCropFunction(File file) {

    // Image Crop Code
    try {




        contentUri = FileProvider.getUriForFile(Addfood_items.this.getContext(),
                BuildConfig.APPLICATION_ID + ".provider",
                file);

        //TODO:  Permission..

        getActivity().getApplicationContext().grantUriPermission("com.android.camera",
                contentUri,
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        CropIntent = new Intent("com.android.camera.action.CROP");
        CropIntent.setDataAndType(contentUri, "image/*");

        CropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        CropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        CropIntent.putExtra("crop", "true");

        CropIntent.putExtra("outputX", 300);
        CropIntent.putExtra("outputY", 300);
        CropIntent.putExtra("aspectX", 3);
        CropIntent.putExtra("aspectY", 3);


        CropIntent.putExtra("scaleUpIfNeeded", true);
        CropIntent.putExtra("return-data", true);


        CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(CropIntent, 1);

    } catch (ActivityNotFoundException e) {

    }
}


public void oldGetImageFromGallery() {


    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

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


    uri = Uri.fromFile(file);

    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

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


    GalIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    final Intent chooserIntent = Intent.createChooser(GalIntent, "Item Image");

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{CamIntent});

    startActivityForResult(chooserIntent, 2);

}


public void GetImageFromGallery() {


    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);


    try {
        File photoFile = createImageFileWith();
        mCurrentPhotoPath = photoFile.getAbsolutePath();
        uri = FileProvider.getUriForFile(Addfood_items.this.getContext(),
                BuildConfig.APPLICATION_ID + ".provider",
                photoFile);


    } catch (IOException e) {
        e.printStackTrace();
    }


    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

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

    GalIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    GalIntent.setType("image/*");



    final Intent chooserIntent = Intent.createChooser(GalIntent, "Item Image");

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{CamIntent});

    startActivityForResult(chooserIntent, 2);

}


public File createImageFileWith() throws IOException {
    final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    final String imageFileName = "JPEG_" + timestamp;
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "shahar");
    storageDir.mkdirs();
    return File.createTempFile(imageFileName, ".png", storageDir);
}


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


    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == 2 && resultCode == Activity.RESULT_OK) {


        if (data != null && data.getData() != null) {


            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {

                File sourceFile = new File(getRealPathFromURI(data.getData()));

                ImageCropFunction(sourceFile);


            } else {

                uri = data.getData();


                oldImageCropFunction();


            }


        } else {


            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {

                Uri IMGuri = Uri.parse(mCurrentPhotoPath);
                File file = new File(IMGuri.getPath());

                ImageCropFunction(file);

            } else {


                oldImageCropFunction();


            }


        }

    } else if (requestCode == 1) {


        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {


            if (data.getData() == null) {

                imagebitmap = (Bitmap) data.getExtras().get("data");
                food_image.setImageBitmap(imagebitmap);
                BitMapToString(imagebitmap);


            } else {
                try {

                    imagebitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
                    food_image.setImageBitmap(imagebitmap);

                    BitMapToString(imagebitmap);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } else {




            Bundle bundle = data.getExtras();

            imagebitmap = bundle.getParcelable("data");
            food_image.setImageBitmap(imagebitmap);
            BitMapToString(imagebitmap);


        }


    }

}


public String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

这里是permisiions

公共类 PermissionManager {

public PermissionManager(){}

private static final String[] PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE ,
        Manifest.permission.READ_EXTERNAL_STORAGE};
private static final int REQUEST_CODE = 101;

public boolean userHasPermission(Context context){
    int permissionCheck = ContextCompat.checkSelfPermission(context, PERMISSIONS[0]);
    int permissionCheck2 = ContextCompat.checkSelfPermission(context, PERMISSIONS[1]);
    int permissionCheck3 = ContextCompat.checkSelfPermission(context, PERMISSIONS[2]);


    return permissionCheck == PackageManager.PERMISSION_GRANTED &&
            permissionCheck2 == PackageManager.PERMISSION_GRANTED &&
            permissionCheck3 == PackageManager.PERMISSION_GRANTED;
}

public void requestPermission(Activity activity){
    ActivityCompat.requestPermissions(activity, PERMISSIONS, REQUEST_CODE);
}

}

4

0 回答 0