如何正确压缩图像并获取URI?

时间:2017-10-05 17:46:14

标签: java android sql thumbnails

我正在尝试将图像和缩略图的URI从相机意图放入SQLite数据库。

我有完整的图像插入就好了。我以为我的缩略图也在进行中,但它实际上仍然是完整的图像再次进入数据库。

我可以获得一些关于如何做到这一点的指导吗?到目前为止,我已经尝试了很多东西,但在这一步中已经停留了几天。

以下是我的相关方法:

 private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        File photoThumbnailFile = null;
        try {
            photoFile = createImageFile();
            photoThumbnailFile = createImageThumbnailFile();
        } catch (IOException ex) {

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            photoURI = FileProvider.getUriForFile(this,
                    "com.example.jeremy.sqlwine.fileprovider",
                    photoFile);

            photoThumbnailURI = FileProvider.getUriForFile(this,
                    "com.example.jeremy.sqlwine.fileprovider",
                    photoThumbnailFile);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private File createImageThumbnailFile() throws IOException {
    // Create an image thumbnail file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageThumbnailFileName = "THUMBNAIL_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageThumbnailFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoThumbnailPath = image.getAbsolutePath();
    return image;
}

//This method will happen when the camera is done taking the picture
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();

        bitmap = null;
        try {

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoURI);
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmapThumbnail = ThumbnailUtils.extractThumbnail(bitmap,50,50);
        imageThumbnail.setImageBitmap(bitmapThumbnail);
    }
}

0 个答案:

没有答案