onActivityResult方法返回空白数据,或者从Gallery中选择图像时不会调用。我的实现在API 16,17 ... 22上运行正常,但我遇到了API 23的问题。我读了这篇文章:https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media,关于权限。
我的客户已经在运行Android 6的3部手机上测试了该应用 并且它们在选择图像时都具有相同的行为。你可以看到一个活动截图,有3个ImageButtons(一个大的,2个小的),当用户点击一个ImageButton时,会触发图库来挑选一个图像,当用户选择一个图像时,ImageButton只是变灰而不是显示挑选的图像。
////onclick method///photo2 is a imagebutton////SELECT_PICTURE2 is the code
photo2_evento.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
//startActivityForResult(Intent.createChooser(intent, "Escoge
una imagen"), SELECT_PICTURE2);
startActivityForResult(intent, SELECT_PICTURE2);
}
});
///onactivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
try{
Uri selectedImageUri = data.getData();
if (requestCode == SELECT_PICTURE1) {
photo1_path = getRealPathFromURI(selectedImageUri);
photo1_evento.setImageBitmap(decodeSampledBitmapFromResource(photo1_path, 400, 400));
}else if(requestCode == SELECT_PICTURE2){
photo2_path = getRealPathFromURI(selectedImageUri);
photo2_evento.setImageBitmap(decodeSampledBitmapFromResource(photo2_path, 100, 100));
}else if(requestCode == SELECT_PICTURE3){
photo3_path = getRealPathFromURI(selectedImageUri);
photo3_evento.setImageBitmap(decodeSampledBitmapFromResource(photo3_path, 100, 100));
}
}catch(Exception e){
e.printStackTrace();
}catch (OutOfMemoryError e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"memory error", Toast.LENGTH_LONG).show();
}
}
}
////decodeSampledBitmapFromResource is just a method provided by Google Android for sizing images.
////this method is for getting the URI path, I got this code from stackoverflow
public String getRealPathFromURI(Uri contentUri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = null;
try {
if (Build.VERSION.SDK_INT > 19) {
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(contentUri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
cursor = Publicar_Eventos.this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, sel, new String[] { id }, null);
} else {
cursor = Publicar_Eventos.this.getContentResolver().query(contentUri,
projection, null, null, null);
}
} catch (Exception e) {
e.printStackTrace();
}catch (OutOfMemoryError e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"error de memoria", Toast.LENGTH_LONG).show();
}
String path = null;
try {
int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index).toString();
cursor.close();
} catch (NullPointerException e) {
e.printStackTrace();
}
return path;
}