将选择的图像复制到设备上的其他文件夹

时间:2019-05-19 15:17:17

标签: android

我刚接触Android开发,这是我关于Stack Overflow的第一篇文章,希望有人能帮助我。

我希望用户选择已存储在设备上的图像,然后将所选图像复制到其他文件夹,在该文件夹中我已经存储了用相机拍摄的图像。 但是,使用我的代码,我最终在首选文件夹中得到了0字节的图像。

我关注了这篇很棒的帖子Copy picked image through Intent.ACTION_PICK to a file,但猜想我错过了什么。

private void pickImage() {
        Intent pickImageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case GALLERY_REQUEST_CODE:
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String selectedImagePath = cursor.getString(columnIndex);
                    cursor.close();
                    Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
                    setReducedImageSize();

                    try {
                        writeBitmapToFile(bitmap, createImageFile());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;

                case CAMERA_REQUEST_CODE:
                    setReducedImageSize();
                    break;
            }
        }
    }

private void setReducedImageSize() {
        int targetImageViewWidth = mCapturedImageView.getWidth();
        int targetImageViewHeight = mCapturedImageView.getHeight();

        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mImageFileLocation, bmOptions);
        int cameraImageWidth = bmOptions.outWidth;
        int cameraImageHeight = bmOptions.outHeight;

        int scaleFactor = Math.min(cameraImageWidth / targetImageViewWidth, cameraImageHeight / targetImageViewHeight);

        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap photoReducedSizeBitmap = BitmapFactory.decodeFile(mImageFileLocation, bmOptions);
        mCapturedImageView.setImageBitmap(photoReducedSizeBitmap);
    }

private boolean writeBitmapToFile(Bitmap bitmap, File destination) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(destination);
            return bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } catch (Exception ex) {
            Log.i(TAG, String.format("Error writing bitmap to %s: %s", destination.getAbsoluteFile(), ex.getMessage()));
            return false;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            }catch (IOException ex) {}
        }
    }

private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "EMPL_" + timeStamp + "_";
        File image = File.createTempFile(imageFileName, ".jpg", mGalleryFolder);

        mImageFileLocation = image.getAbsolutePath();
        return image;
    }

1 个答案:

答案 0 :(得分:0)

替换:

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String selectedImagePath = cursor.getString(columnIndex);
                cursor.close();
                Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);

具有:

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));