我正在编写一个应用程序,其中用户将点击应用程序中的按钮,图库将弹出,用户将从库中选择一个图像,所选图像应该显示在imageView中我放在屏幕上。当我单击按钮弹出库时,我选择了一个图像,但出于某种原因,该图像没有显示在我的应用程序屏幕上。非常感谢您的帮助。我只添加了两个权限,如下所示:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPicture(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
Uri photoUri = data.getData();
Bitmap selectedImage = null;
try{
selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
} catch (Exception e){
e.printStackTrace();
}
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(selectedImage);
}
}
}
编辑:我尝试了答案中给出的代码,但是我的堆栈跟踪中出现以下错误。为简洁起见,仅显示RuntimeExceptions:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/46 }} to activity {cs193a.stanford.edu.testproject/cs193a.stanford.edu.testproject.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/46 from pid=2754, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/46 from pid=2754, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
在我的代码中似乎有问题的唯一代码行再次是声明Cursor的行。非常感谢您的帮助
答案 0 :(得分:0)
打开图库
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
取回结果:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent imageReturnedIntent) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case RESULT_LOAD_IMAGE:
if(resultCode == RESULT_OK)
{
Uri uri = imageReturnedIntent.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap image = BitmapFactory.decodeFile(filePath);
ImageView im = (ImageView)findViewById(R.id.imagev);
im.setImageBitmap(image);
}
}
}
编辑:请立即尝试