我正在尝试将图像从Android上传到远程服务器。我尝试了 以下代码,但得到空指针异常。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
//Bitmap photo = (Bitmap) data.getData().getPath();
Uri selectedImageUri = data.getData();
imagepath = getPath(FileUploadActivity.this,selectedImageUri);
Bitmap bitmap= null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imageview.setImageBitmap(bitmap);
messageText.setText("Uploading file path:" +selectedImageUri);
}
}
public String getPath(Context context, Uri uri) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
return result;
}
在int列int column_index = cursor.getColumnIndexOrThrow(proj [0]); column_index的值即将变为0。我不确定为什么会这样。任何人都可以在这里找出问题所在。预先谢谢你。