帮助我将图像按钮的 uri 设置为我从内部存储中选择的图像。我已经在 android_manifest.xml 文件中提供了所需的权限。问题是当我浏览到手机的内部存储时,无法选择图像。
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
最佳答案
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
然后在你的 Activity 结果方法中保留这个
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
编辑
是的,问题是我忘了添加 .Media 所以使用这行代码:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
关于android - 如何将图像按钮设置为我的内部存储(Android)中的某些图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47069444/