Eamorr
Eamorr

Reputation: 10022

Android upload image

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,

Upvotes: 2

Views: 2987

Answers (1)

Will Tate
Will Tate

Reputation: 33509

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

Upvotes: 3

Related Questions