g.b.
g.b.

Reputation: 3

File path to String

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;
             }
        }
}

Upvotes: 0

Views: 6429

Answers (3)

RobinHood
RobinHood

Reputation: 10969

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.

Upvotes: 0

Athul Harikumar
Athul Harikumar

Reputation: 2491

 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;
         }
    }
 }

Upvotes: 0

Shiv
Shiv

Reputation: 4567

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 ...

Upvotes: 1

Related Questions