我正在尝试使用内置图库获取图像。它在模拟器中运行良好,它只打开图库但在真实设备上它给我多个选择其中一个是文件管理器,这使我能够选择任何类型的文件甚至apk文件当然应用程序崩溃后 我有这个代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode){
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
break;
}
}
}
答案 0 :(得分:24)
尝试使用
....
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
....
答案 1 :(得分:5)
public void ChoosePicture(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bMap_image = BitmapFactory.decodeFile(filePath);
ImageView img = (ImageView) findViewById(R.id.gallery1);
img.setImageBitmap(bMap_image);
}catch(Exception e)
{}
}
}// resultCode
}// case 1
}// switch, request code
}// public void onActivityResult
mmh,不知怎的,它改变了我最后几个“}”的位置。
此代码可让您从图库中选择图像,然后在图像视图中显示。
我在设备上使用此代码,就像一个魅力。
答案 2 :(得分:1)
尝试将此用于您的意图:
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
如果您想要始终使用库存应用程序,我认为您不需要使用Intent Chooser,因此您可以将startActivity更改为:
startActivityForResult(intent, SELECT_PICTURE);