1

This is how I open gallary

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);

This is my onActvitityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Disc.setText(selectedImagePath);
            }
        }
    }

This is how im trying to get the real path from gallary but its returning nothing, String its returning is empty.

private String getPath(Uri uri) {
        // just some safety built in
        if( uri == null ) {
            Toast.makeText(this, "Error problem with system", Toast.LENGTH_SHORT).show();
            return null;
        }
        String path = "No-Path-Found";
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if(cursor.moveToFirst()){
            int column_index = cursor
                    .getColumnIndex(MediaStore.Images.Media.DATA);
            path = cursor.getString(column_index);
        }
            cursor.close();
            return path;
    }

Can anyone help me with this, I been searching online but nothing is working

SpriteAndDreams
  • 50
  • 1
  • 10
  • in order to get right from Uri you can use this: [Get real path from URI of file](https://stackoverflow.com/questions/42110882/get-real-path-from-uri-of-file-in-sdcard-marshmallow) – Hooman Oct 01 '17 at 21:47

2 Answers2

0

A Uri is not a file. There is no requirement that ACTION_GET_CONTENT return to you a Uri that is from MediaStore. There is no requirement that ACTION_GET_CONTENT return to you a Uri that points to a file at all.

Delete your getPath() method. Use a ContentResolver and openInputStream() to read in the content identified by the Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How do I do that? Im pretty lost – SpriteAndDreams Oct 01 '17 at 21:37
  • @SpriteAndDreams: Call `getContentResolver()` on a `Context`, such as an `Activity` or `Service`. Call `openInputStream()` on the `ContentResolver`, passing in the `Uri`. This returns an `InputStream`, which you use with standard Java I/O to read in the bytes. Or, depending on what you are doing overall, you might just hand the `Uri` off to a library (e.g,. Glide or Picasso for loading images). – CommonsWare Oct 01 '17 at 21:56
0

Found out that you need to use runtime permision for android api 19 and above, it works perfectly now:)

SpriteAndDreams
  • 50
  • 1
  • 10