将选定的图像复制到设备上的不同文件夹

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (13)

我对Android开发很新,这是我在Stack Overflow上的第一篇文章,我希望有人可以帮助我。

我希望用户选择已存储在设备上的图像,并将所选图像复制到另一个文件夹,我已经存储了使用相机拍摄的图像。但是使用我的代码,我最终会在首选文件夹中显示0字节的图像。

我按照这帖子通过Intent.ACTION_PICK复制挑选的图像到一个文件,但我猜我丢失了一些代码。

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;
    }
提问于
用户回答回答于

更换:

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

扫码关注云+社区

领取腾讯云代金券