2

Greetings,

I'm trying to upload an image on my Android device.

I have the following code which causes the image picker to come up and examine the SD card (which is what I want).

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);

But what do I do next? Is there some class I can override to handle the image that was clicked on? I've tried googling but can't find anything. I hope someone can point me in the right direction.


Tried taking willytate's advice, but now I keep getting the following error:

02-08 16:58:01.574: ERROR/AndroidRuntime(16962): FATAL EXCEPTION: WebViewCoreThread
02-08 16:58:01.574: ERROR/AndroidRuntime(16962): java.lang.NullPointerException
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.app.Activity.startActivityForResult(Activity.java:2890)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at prestocab.driver.Main$Uploader.upload(Main.java:172)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.webkit.WebViewCore.nativeTouchUp(Native Method)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.webkit.WebViewCore.access$3300(WebViewCore.java:66)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1226)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.os.Looper.loop(Looper.java:143)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:642)
02-08 16:58:01.574: ERROR/AndroidRuntime(16962):     at java.lang.Thread.run(Thread.java:1102)

Here is my class:

public class Uploader extends Activity{   //uploader
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
        }
        public void upload(){
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent,0);

        }
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = data.getDataString();
                Toast.makeText(Main.this, selectedImagePath, 2000);
            }
        }
    }

Many thanks in advance,

1
  • Hey, I ended up having to get a rentacoder... Commented May 19, 2011 at 22:10

1 Answer 1

3

Eamorr,

You will need to catch the returned image in onActivityResult(). You will have the check the requestCode value to make sure it was the intent you wanted that returned. In your case you put a 1. Then handle the image upload...like so:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch(requestCode) {
        case 1:
            imageUri = data.getData();  //get the Uri of the image from the returned data
            //upload imageUri
            break;
        }
    }
}

EDIT: Eamorr.

Use this function to get the ImagePath string insted of data.getDataString(). Just pass in imageUri.

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

I actually have the same functionality in my app so I've been through all these hurdles ><

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

2 Comments

Hey, thanks for that. I think I'm getting somewhere now. I have an "Uploader" subclass. I've just extended it from "Activity" and I'm hoping things will work the way I plan! Cheers from Ireland,
Hey willytate, just edited my post. If you've any more insight, it would be greatly appreciated.

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.