Android只从图库中获取图片

mrl*_*l25 12 android gallery

我正在尝试使用内置图库获取图像.它在模拟器中运行良好,它只打开图库但在真实设备上它给我多个选择其中一个是文件管理器,这使我能够选择任何类型的文件甚至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;
        }  
      }  
Run Code Online (Sandbox Code Playgroud)

}

Vya*_*kin 25

尝试使用

.... 
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE);
....
Run Code Online (Sandbox Code Playgroud)

  • 股票Android 4.4照片应用程序不考虑这一点. (2认同)

Big*_*low 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
Run Code Online (Sandbox Code Playgroud)

mmh,不知怎的,它改变了我最后几个"}"的位置.

此代码将允许您从库中选择图像,然后在imageview上显示它.

我在我的设备上使用这个代码,就像一个魅力.