使用Intent.ACTION_PICK

问题描述

嗨,我在重新打印图像时遇到问题。

我第一次选择图像,一切正常。 当我尝试重新复制图像并且我选择的图像没有出现并且没有位图数据时,问题就开始了。

enter image description here

点击按钮选择图片

  imagePickerAddBtn.setonClickListener(v -> {
            Intent chooseImageIntent = ImagePicker.getPickImageIntent(mActivity);
            chooseImageIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivityForResult(chooseImageIntent,pickImageId);
        });

ImagePicker.class

public static Intent getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra("return-data",true);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile(context)));
    intentList = addIntentsToList(context,intentList,pickIntent);
    intentList = addIntentsToList(context,takePhotoIntent);

    if (intentList.size() > 0) {
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),context.getString(R.string.image_pickerOptions));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentList.toArray(new Parcelable[]{}));
    }

    return chooserIntent;
}

处理选择器的结果

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if (resultCode != 0) {
        switch (requestCode) {
            case NewRequestActivity.PICK_IMAGE_ID_1:
                onActivityResultimagePicker(0,imagePicker1,imagePickerDeleteBtn1,data,imagePickerAddBtn1);
                break;
            case NewRequestActivity.PICK_IMAGE_ID_2:
                onActivityResultimagePicker(1,imagePicker2,imagePickerDeleteBtn2,imagePickerAddBtn2);
                break;
            case NewRequestActivity.PICK_IMAGE_ID_3:
                onActivityResultimagePicker(2,imagePicker3,imagePickerDeleteBtn3,imagePickerAddBtn3);
                break;
            default:
                super.onActivityResult(requestCode,data);
                break;
        }
    }
}

private void onActivityResultimagePicker(int index,ImageButton imagePicker,ImageButton imagePickerDeleteBtn,Intent data,ImageButton imagePickerAddBtn) {
    imagePickerAddBtn.setVisibility(View.GONE);
    imagePickerDeleteBtn.setVisibility(View.VISIBLE);
    imagePicked.add(index,ImagePicker.getimageFromresult(mActivity,data));
    imagePicker.setimageBitmap(imagePicked.get(imagePicked.size() - 1));
}

getimageFromresult

public static Bitmap getimageFromresult(Context context,Intent imageReturnedIntent) {
    Log.d(TAG,"getimageFromresult,resultCode: " + resultCode);
    Bitmap bm = null;
    File imageFile = getTempFile(context);
    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage;
        boolean isCamera = (imageReturnedIntent == null ||
                imageReturnedIntent.getData() == null  ||
                imageReturnedIntent.getData().toString().contains(imageFile.toString()));
        if (isCamera) {     /** CAMERA **/
            selectedImage = Uri.fromFile(imageFile);
        } else {            /** ALBUM **/
            selectedImage = imageReturnedIntent.getData();
        }
        Log.d(TAG,"selectedImage: " + selectedImage);

        bm = getimageResized(context,selectedImage);
        int rotation = getRotation(context,selectedImage,isCamera);
        bm = rotate(bm,rotation);
    }
    return bm;
}

调整大小,以避免使用过多的内存来加载大图像(例如:2560 * 1920)

private static Bitmap getimageResized(Context context,Uri selectedImage) {
    Bitmap bm = null;
    int[] sampleSizes = new int[]{5,3,2,1};
    int i = 0;
    do {
        bm = decodeBitmap(context,sampleSizes[i]);
        Log.d(TAG,"resizer: new bitmap width = " + bm.getWidth());
        i++;
    } while (bm.getWidth() < DEFAULT_MIN_WIDTH_QUALITY && i < sampleSizes.length);
    return bm;
}


private static Bitmap decodeBitmap(Context context,Uri theUri,int sampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;

    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri,"r");
    } catch (FileNotFoundException e) {
        e.printstacktrace();
    }

    Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
            fileDescriptor.getFileDescriptor(),null,options);

    Log.d(TAG,options.inSampleSize + " sample method bitmap ... " +
            actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight());

    return actuallyUsableBitmap;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)