从图库中选择图像时未获得正确的路径

时间:2017-05-18 21:16:26

标签: java android image gallery photo

当我通过相机拍摄图像时,它工作正常,我得到了保存在SQLite数据库中的图像路径,并可以检索显示所需图像的路径。但是当我从Gallery中选择一个图像时,我没有得到正确的路径,我需要将其保存在数据库中以便进一步检索。当我在数据库中保存路径时,我没有得到图像导致路径不正确。所以我的问题是 - 我怎样才能获得画廊图像的正确绝对路径?

  @RequiresApi(api = Build.VERSION_CODES.N)
private void onCaptureImageResult(Intent data) {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    Bitmap bm = BitmapFactory.decodeFile(destination.getAbsolutePath());
    ivImage.setImageBitmap(bm);
    showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
    showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
    ivImage.setImageBitmap(bm);
}

在这里,我在ImageView ivImage中显示正确的图像,同时通过相机或图库拍摄图像。但是当我从Gallery中选择一个图像(我拿相机图像时工作正常)时,我没有正确地获得路径,我将其保存在数据库中以进行进一步的路径检索。

2 个答案:

答案 0 :(得分:1)

  

我如何获得这个选定的图像路径

对于感觉像第3,735次的情况,没有"选择的图像路径"。您正在获得Uri返回(data.getData()); Uri不必指向文件。例如,http://stackoverflow.com/questions/44057912/not-getting-correct-path-when-select-image-from-galleryUri;它不代表文件系统上的文件。

如果您需要文件,请使用a file-picker library

答案 1 :(得分:0)

最后,我决定执行与在“onCaptureImageResult(Intent data)”中所做的相同的过程。我从画廊获得的图像再次存储到一个目录中。我知道这绝对不是一个好的程序,但它在时间上解决了我的问题,直到我得到一个好的答案。

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bm1 = BitmapFactory.decodeFile(destination.getAbsolutePath());
    ivImage.setImageBitmap(bm1);
    String selectedImageUri = data.getData().getPath();
    showImagepathET.setText(destination.getAbsolutePath());

}
相关问题