您好我是android的初学者。我正在编写代码来从图库中选择图像。我的代码在Nexus_S模拟器上使用Lollipop 5.1.1正常工作,但不能在设备上工作(Motorola E2 with Lollipop 5.1.1) 这是我的代码,
@Override
public void onClick(View v)
{
if(v.getId()==R.id.layout_pickImage)
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, Utilities.REQUEST_ADD_IMAGE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Utilities.REQUEST_ADD_IMAGE)
{
if(data!=null)
{
final Bundle extras = data.getExtras();
if (extras != null)
{
try{
Uri uri = data.getData();
selected_img_bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imgv_selected.setImageBitmap(selected_img_bitmap);
imgv_selected.setVisibility(View.VISIBLE);
}catch(Exception e)
{
Toast.makeText(this, "error "+e, Toast.LENGTH_SHORT).show();
}
}
}
}
}
答案 0 :(得分:0)
为什么你需要额外的东西?你没有在任何地方使用它。只需删除那些额外内容即可。
onActivityResult
看起来像这样:
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data ) {
super.onActivityResult( requestCode, resultCode, data );
if ( requestCode == Utilities.REQUEST_ADD_IMAGE ) {
if ( data != null ) {
try {
Uri uri = data.getData();
selected_img_bitmap = MediaStore.Images.Media.getBitmap( getContentResolver(), uri );
imgv_selected.setImageBitmap( selected_img_bitmap );
imgv_selected.setVisibility( View.VISIBLE );
} catch ( Exception e ) {
Toast.makeText( this, "error " + e, Toast.LENGTH_SHORT ).show();
}
}
}
}
如果它能解决您的问题,请告诉我