Android upload image

本秂侑毒 提交于 2019-12-10 23:09:56

问题


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:


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



来源:https://stackoverflow.com/questions/4935683/android-upload-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!