我尝试允许应用用户从其图库中选择图片,并且只有在用户实际选择图片时才设置图片。我面临的问题是,当我打开图像库时,所有图像都显示为灰色,无论图像格式如何,我都无法选择图像。以下是该部分的代码:
hostImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent imageIntent = new Intent();
imageIntent.setType("image*/");
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(imageIntent,"Choose host image"),000);
}
}
);
}
public void onActivityResult(int askCode, int retCode, Intent info){
if (retCode==RESULT_OK){
if (askCode==000){
hostImageView.setImageURI(info.getData());
}
}
答案 0 :(得分:1)
更新图库开放的代码,如下所示:
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
在2
方法
activity
(或您用于启动onActivityResult
的数字)
答案 1 :(得分:0)
我建议您获取所选的图像路径而不是图像类型,然后将其加载到图像视图中。试试这个,
public boolean click(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
答案 2 :(得分:0)
在正斜杠(/).
之后应该给出星号(*)内容类型“image / *”表示类别图片,包含任何扩展名(.jpeg,.png,...)
以下为我工作
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);