views:

66

answers:

1

I am currently making an app which works with images. I need to implement functionality where the user picks a file stored on the SD card. Once they pick the picture (using the Android gallery), the the file-location of the image will be sent to another Activity, where other work will be done upon it.

I have seen similar posts here on SO, but none to answer my question specifically. Basically this is the code I am doing when the user clicks the "Load a Picture" button:

// Create a new Intent to open the picture selector:
Intent loadPicture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

// To start it, run the startActivityForResult() method:
startActivityForResult(loadPicture, SELECT_IMAGE);

From that code, I then have a onActivityResult() method to listen to the call-back:

// If the user tried to select an image:
if(requestCode == SELECT_IMAGE)
{
    // Check if the user actually selected an image:
    if(resultCode == Activity.RESULT_OK)
    {
        // This gets the URI of the image the user selected:
        Uri selectedImage = data.getData();

        // Create a new Intent to send to the next Activity:
        Intent i = new Intent(currentActivty.this, nextActivity.class);

        // ----------------- Problem Area -----------------
        // I would like to send the filename to the Intent object, and send it over.
        // However, the selectedImage.toString() method will return a
        // "content://" string instead of a file location.  How do I get a file
        // location from that URI object?
        i.putExtra("PICTURE_LOCATION", selectedImage.toString());

        // Start the activity outlined with the Intent above:
        startActivity(i);

As the code above states, the uri.toString() will return a content:// string instead of the file location of the selected picture. How do I obtain the file location?

Note: Another possible solution is to send over the content:// string and convert that into a Bitmap (which is what happens in the next Activity). However, I don't know how to do that.

A: 

I have found the answer to my own question. After doing some more searching, I finally stumbled upon a post here on SO which asks the same question here: http://stackoverflow.com/questions/2789276/android-get-real-path-by-uri-getpath.

Unfortunately, the answer has a broken link. After some Google searching, I found the correct link to the site here: http://www.androidsnippets.org/snippets/130/ (I have verified that this code does indeed work.)

However, I decided to take a different route. Since my next Activity is using an ImageView to display the picture, I am instead going to use the Uri content string for all methods that link to the next Activity.

In the next Activity, I am using the ImageView.setImageUri() method.

Here is the code I am doing in the next Activity to display the picture from the content:// string:

// Get the content string from the previous Activity:
picLocation = getIntent().getStringExtra("PICTURE_LOCATION");

// Instantiate the ImageView object:
ImageView imageViewer = (ImageView)findViewById(R.id.ImageViewer);

// Convert the Uri string into a usable Uri:
Uri temp = Uri.parse(picLocation);
imageViewer.setImageURI(temp);

I hope that this question and answer will be helpful to future Android developers.

Phanto