0

In my application, I'm opening the Android default gallery to select and image. The result is the path.

When I test the application in my phone, the path is /mnt/sdcard/picturename but when I test it in my collegue's phone, the path is /sdcard/picturename

This causes a problem because later in my code, I use a substring method to collect only the picturename. and it gives different results depending on whether the path contains /mnt/ or not!

Do u happen to know how can we get the path starting with /sdcard/ ... ??

here is the code I used to open the Gallery :

photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cheminNouvellePhoto.setText("/sdcard/images/..");
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), 101);
            }
        });

and the code that returns the path :

public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
ashabasa
  • 311
  • 2
  • 9
  • 20

3 Answers3

0

You should access pictures in the external storage using this Environment.getExternalStorageDirectory() as the URL might vary from device to device.

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
  • I'v just added my code to the question, where exactly should Environment.getExternalStorageDirectory() be ? – ashabasa May 07 '13 at 15:30
  • Maybe the problem is not here, can you add the function `public void onActivityResult` you use and also where you do your substring? I will modify my answer. – Yoann Hercouet May 07 '13 at 15:48
0

You should not hard code this path in your app, you can use the following code to get the right path on every device:

Environment.getExternalStorageDirectory()

If what you need to get is the photo name, you can get it when you get the result of the picture selection activity, here is the beginning of the function you could use:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RateDayApplication.SELECT_PICTURE_DIALOG_ID) {
        if (resultCode == getActivity().RESULT_OK) {
            //get Uri from the intent
            Uri selectedImageUri = data.getData();

            //get the image path
            String selectedImagePath = getPath(selectedImageUri);

            if(selectedImagePath!=null) {
                //get the image selected
                File photoSelected = new File(selectedImagePath);
                //get the name of the image
                String photoNameString = photoSelected.getName();
Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
0

I think it has to do with some devices having a built-in 'sd card' and others having a real sd card in a slot, the path to the external storage my vary. You shouldn't work with absolute paths anyway, check this question on how to get the path to the external storage.

Community
  • 1
  • 1
fweigl
  • 21,278
  • 20
  • 114
  • 205