我希望我的應用能夠從Android圖庫中選取圖像並將其上載到服務器上。而代碼在API lenel 22即棒棒糖上效果很好。但是在API Level 23上失敗並出現NullPointerException。 API 23之後是否有任何改變?如果是這樣的話。將Android圖像上傳至服務器Api Api 23及以上版本
這是我使用的代碼。
public void onClickAddProfileImage(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PROFILE_PHOTO);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PROFILE_PHOTO && resultCode == Activity.RESULT_OK) {
uploader.setVisibility(View.VISIBLE);
Uri selectedImage = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(this, selectedImage, projection, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
Uri imageUri = Uri.parse(cursor.getString(column_index));
cursor.close();
Intent in = new Intent();
UploadFile file_upload = new UploadFile(this, in);
file_upload.execute();
}
}
而且錯誤日誌我上線
Uri imageUri = Uri.parse(cursor.getString(column_index));
得到的是
Process: com.webkul.taskapp, PID: 2648 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:205520 flg=0x1 }} to activity {com.webkul.taskapp/com.webkul.taskapp.BaseActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:475)
at android.net.Uri$StringUri.<init>(Uri.java)
at android.net.Uri.parse(Uri.java:437)
at com.webkul.taskapp.BaseActivity.onActivityResult(BaseActivity.java:535)
at android.app.Activity.dispatchActivityResult(Activity.java:6442)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
請任何人提供任何建議來解決它。
所以這時光標== NULL解決它。或cursor.getString()== null。如果您在使用前檢查,您將不會有這種例外。 – greenapps
'selectedImage'的類型是什麼? – greenapps
'Uri imageUri'。你沒有使用'imageUri'。 – greenapps