【问题标题】:Bitmap to uri conversion android位图到uri转换android
【发布时间】:2019-11-15 15:27:32
【问题描述】:

从图库中选择裁剪图像后如何从位图中获取uri

我试过了

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

但是 uri = null

我想从中获取 uri

if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
            Uri imageUri = data.getData();
            performCrop(imageUri);
            Log.e(TAG, "image before crop:" + imageUri);

        }else if(resultCode == Activity.RESULT_OK && requestCode == PIC_CROP ){
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");
            getImageUri(this,selectedBitmap);
            Uri ImageUri = data.getData();
            Log.e(TAG, "image before crop:" + ImageUri);

在crop=null之前记录图像

---- 更新裁剪----

   private void performCrop(Uri picUri) {
try {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    // indicate image type and Uri
    cropIntent.setDataAndType(picUri, "image/*");
    // set crop properties here
    cropIntent.putExtra("crop", true);
    // indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    // indicate output X and Y
    cropIntent.putExtra("outputX", 128);
    cropIntent.putExtra("outputY", 128);
    // retrieve data on return
    cropIntent.putExtra("return-data", true);
    // start the activity - we handle returning in onActivityResult
    startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    // display an error message
    String errorMessage = "Whoops - your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}

【问题讨论】:

    标签: java android bitmap uri


    【解决方案1】:

    我猜你正在从你的图库中选择一张图片。

    福利:

    • 清单中读取/写入数据的权限
    • 验证权限并处理未授予权限的情况
    • 请求代码声明:

      public static final int PICK_IMAGE = 1050;
      

    (1) 打开图片选择器活动

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    

    活动中:

    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    

    在片段中:

    fragment.startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    

    (2) 从选择中检索图像:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
    
        if(resultCode == RESULT_OK && data != null && data.getData() != null ){
    
        switch(requestCode){
    
            case PICK_IMAGE_REQUEST:
                //get filepath from the result of image selection
                Uri filePath = data.getData();
                //Start activity for result for crop for selected image
                startCropActivity(filePath);
                break;
    
            case PIC_CROP:
                // get the returned data
                Bundle extras = data.getExtras();
                // get the cropped bitmap
                Bitmap selectedBitmap = extras.getParcelable("data");
                //do whatever with the bitmap of the image
                break;
    
                }
            }
    }
    

    像这样开始裁剪活动:

    private void startCropActivity(Uri filePath){
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            cropIntent.setDataAndType(filePath, "image/*");
            cropIntent.putExtra("crop", true);
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", 128);
            cropIntent.putExtra("outputY", 128);
            cropIntent.putExtra("return-data", true);
    
            startActivityForResult(cropIntent, PIC_CROP);
    
        }catch (ActivityNotFoundException anfe) {
            // display an error message
            Toast.makeText(this, "Could not crop", Toast.LENGTH_SHORT).show();
    
        }
    }
    

    【讨论】:

    • @OsamahM 我已经更新了我的答案......裁剪的结果会产生裁剪图像的位图..取决于你想要做什么..你想保存图像吗?或将其设置为图像视图等。
    • @OsamahM 日志是否说明了严格模式?
    • 我需要将图像保存为 temp 并获取 uri,裁剪后需要将图像添加到应用程序中,请参阅此处我如何将图像添加到应用程序而不进行裁剪,只需从图库@987654327 中选择@我需要在裁剪后也能正常工作,所以可能需要先保存图像才能获取 uri。 @haider-malik
    • 你能帮我解决这个问题吗@haider-malik
    猜你喜欢
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2018-04-23
    • 2012-01-07
    • 1970-01-01
    相关资源
    最近更新 更多