0

I'm trying to get the complete path of an image file as a String, but it doesn't work. I'm just getting a result like "content:/external/media/images/1". That's definitely not the correct path. How can i get the correct path including the file extensions?

Here is what i tried so far:

       public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.btnGetImage:
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                     intent.setType("image/*");
                     startActivityForResult(Intent.createChooser(intent, "Select A Picture"),  
                               PHOTO_GALLERY);
                    break;
         }
 }

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         switch(requestCode){
         case PHOTO_GALLERY:
             if (resultCode == RESULT_OK) {
                 File file = new File(data.getDataString());
                 String imagePath = file.getAbsolutePath();
                 break;
             }
        }
}

3 Answers 3

1

try this code:

case R.id.btnGetImage:

        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
        break;

and

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        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]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

picturePath is your required path ...

Sign up to request clarification or add additional context in comments.

Comments

0
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     switch(requestCode){
     case PHOTO_GALLERY:
         if (resultCode == RESULT_OK) {
             Uri selectedImageUri = data.getData();
        String selectedImagePath = getRealPathFromURI(selectedImageUri);
             File file = new File(selectedImagePath );

             break;
         }
    }
 }

1 Comment

hav u tried this code it will work if the intent is coming from gallery if from a file manager you ll get the absolute path itself
0

You have define correct filename to your path which must be exist in your sdcard, while fetch from sdcard,

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+test.png;

I smell you trying to do something like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.