Open gallery app from Android Intent

后端 未结 6 1168
难免孤独
难免孤独 2020-11-29 04:30

I\'m looking for a way to open the Android gallery application from an intent.

I do not want to return a picture, but rather just open the gallery to al

相关标签:
6条回答
  • 2020-11-29 05:07

    This is what you need:

    ACTION_VIEW
    

    Change your code to:

    Intent intent = new Intent();  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    intent.setType("image/*");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-29 05:10
    1. Following can be used in Activity or Fragment.

       private File mCurrentPhoto;
      
    2. Add permissions

      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"  />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
      
    3. Add Intents to open "image-selector" and "photo-capture"

      //A system-based view to select photos.
      private void dispatchPhotoSelectionIntent() {
      Intent galleryIntent = new Intent(Intent.ACTION_PICK,
              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR);
      }
      
      
      
          //Open system camera application to capture a photo.
          private void dispatchTakePictureIntent() {
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      // Ensure that there's a camera activity to handle the intent
      if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) {
          try {
              createImageFile();
          } catch (IOException ex) {
              // Error occurred while creating the File
          }
          // Continue only if the File was successfully created
          if (mCurrentPhoto != null) {
              takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto));
              startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
          }
      }
      }
      
    4. Add handling when getting photo.

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (requestCode) {
      case REQUEST_IMAGE_SELECTOR:
          if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
              String[] filePathColumn = { MediaStore.Images.Media.DATA };
              Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
              if (cursor == null || cursor.getCount() < 1) {
                  mCurrentPhoto = null;
                  break;
              }
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              if(columnIndex < 0) { // no column index
                  mCurrentPhoto = null;
                  break;
              }
              mCurrentPhoto = new File(cursor.getString(columnIndex));
              cursor.close();
          } else {
              mCurrentPhoto = null;
          }
          break;
      case REQUEST_IMAGE_CAPTURE:
          if (resultCode != Activity.RESULT_OK) {
              mCurrentPhoto = null;
          }
          break;
      }
      if (mCurrentPhoto != null) {
          ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
          Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
      }
      super.onActivityResult(requestCode, resultCode, data);
      }
      
    0 讨论(0)
  • 2020-11-29 05:11

    I hope this will help you. This code works for me.

    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(i, RESULT_LOAD_IMAGE);
    

    ResultCode is used for updating the selected image to Gallery View

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
    
            if (cursor == null || cursor.getCount() < 1) {
                return; // no cursor or no record. DO YOUR ERROR HANDLING
            }
    
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    
            if(columnIndex < 0) // no column index
                return; // DO YOUR ERROR HANDLING
    
            String picturePath = cursor.getString(columnIndex);
    
            cursor.close(); // close cursor
    
            photo = decodeFilePath(picturePath.toString());
    
            List<Bitmap> bitmap = new ArrayList<Bitmap>();
            bitmap.add(photo);
            ImageAdapter imageAdapter = new ImageAdapter(
                    AddIncidentScreen.this, bitmap);
            imageAdapter.notifyDataSetChanged();
            newTagImage.setAdapter(imageAdapter);
        }
    
    0 讨论(0)
  • 2020-11-29 05:14

    if somebody still getting the error, even after adding the following code

    Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

    then you might be opening the intent in another class (by calling) due to this the app will crash when you click the button.

    SOLUTION FOR THE PROBLEM

    Put intent in the class file which is connected to the button's layout file. for example- activity_main.xml contains the button and it is connected to MainActivity.java file. but due to fragmentation, you are defining the intent in some other class(lets says Activity.java) then your app will crash unless you place the intent in MainActivity.java file. I was getting this error and it is resolved by this.

    0 讨论(0)
  • 2020-11-29 05:20

    As you don't want a result to return, try following simple code.

    Intent i=new Intent(Intent.ACTION_PICK);
                i.setType("image/*");
                startActivity(i);
    
    0 讨论(0)
  • 2020-11-29 05:23

    You can open gallery using following intent:

    public static final int RESULT_GALLERY = 0;
    
    Intent galleryIntent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent , RESULT_GALLERY );
    

    If you want to get result URI or do anything else use this:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        switch (requestCode) {
        case QuestionEntryView.RESULT_GALLERY :
            if (null != data) {
                imageUri = data.getData();
                //Do whatever that you desire here. or leave this blank
    
            }
            break;
        default:
            break;
        }
    }
    

    Tested on Android 4.0+

    0 讨论(0)
提交回复
热议问题