首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将拾取的图像复制到设备上的不同文件夹

将拾取的图像复制到设备上的不同文件夹
EN

Stack Overflow用户
提问于 2019-05-19 23:17:17
回答 1查看 21关注 0票数 0

我是Android开发的新手,这是我在Stack Overflow上的第一篇文章,我希望有人能帮我。

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

我关注了这个很棒的帖子Copy picked image through Intent.ACTION_PICK to a file,但我猜我错过了一些东西。

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-19 23:27:17

替换:

代码语言:javascript
复制
                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);

通过以下方式:

代码语言:javascript
复制
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56209166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档