Java 从gallery中选择图像在android中不起作用
我正在尝试从gallery中选择要在imageview中显示的图像,该选择起作用,但图像未显示在imageview中,它仍为空 代码: 我做错了什么?试试下面的代码Java 从gallery中选择图像在android中不起作用,java,android,image-gallery,photo-gallery,Java,Android,Image Gallery,Photo Gallery,我正在尝试从gallery中选择要在imageview中显示的图像,该选择起作用,但图像未显示在imageview中,它仍为空 代码: 我做错了什么?试试下面的代码 iv.setImageBitmap(yourSelectedImage); Drawable d=new BitmapDrawable(yourSelectedImage); iv.setBackground(d); 而不是下面的代码 iv.setImageBitmap(yourSelectedImage); Drawa
iv.setImageBitmap(yourSelectedImage);
Drawable d=new BitmapDrawable(yourSelectedImage);
iv.setBackground(d);
而不是下面的代码
iv.setImageBitmap(yourSelectedImage);
Drawable d=new BitmapDrawable(yourSelectedImage);
iv.setBackground(d);
检查这个例子
我只是试了一下,它就起作用了
希望有帮助,祝你好运。我曾经遇到过同样的问题,它是由
Uri
从data.getData()
返回的文件路径有时为空引起的。因此,这会导致图像为null
。以下是我所做的:
首先,我没有从Uri
获取文件路径,但我使用Uri
本身,使用以下方法为我提供图像
private Bitmap getBitmapFromUri(Uri uri)throws IOException{
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(uri);
return BitmapFactory.decodeStream(inputStream, null, options);
}
您可以在选项中传递null。该方法抛出IOException
,因此请确保捕获该异常。
因此,当您想在图像视图中使用它时:
imageView.setImageBitmap(getBitmapFromUri(data.getData()));
还要记住如何调整图像大小,因为用户可以选择非常大的图像,因此占用内存。还可以修改代码来调整大小 首先确保在manifest.xml文件上声明了所有必要的权限。那就试试吧。我已经编辑了你的代码-
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PICTURE=1;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
iv=(ImageView)findViewById(R.id.imageView1);
//FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//fab.setOnClickListener(new View.OnClickListener() {
//@Override
//public void onClick(View view) {
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//.setAction("Action", null).show();
//}
}//);
public void btnClick(View v){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECTED_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECTED_PICTURE:
if(resultCode==RESULT_OK){
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
cursor.close();
iv. setImageBitmap(bitmap);
}
break;
default:
break;
}
}
}
关于imageView.setImageBitmap(bitmap)
methodtryiv.setImageDrawable(d)代码>位于iv.立根点(d)处代码>尝试此库它处理与拾取图像或拍照相关的所有混乱,即权限、方向问题等