Android的获得仅图库图片图库、图片、Android

2023-09-07 16:32:13 作者:惆怅暮烟垂

我试图让使用内置的画廊图像。它工作正常,在模拟器中它仅打开图片库,但真实设备,它给我多选其中之一就是文件管理器这使我选择任何类型的文件,甚至APK当然文件后,该应用程序崩溃我有这个code

I’m trying to get an image using the built in gallery. It works fine in the emulator and It opens only gallery but on real device it give me multiple chooses one of them is file manager which enable me to choose any type of files even apk files of course the app crash after that I have this code

@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;
        }  
      }  

}

推荐答案

尝试使用

.... 
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE);
....