0

I am trying to open the gallery app from my android application. I want to just open the gallery app and not other apps with like fotos.

I have tried

intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");

and

 intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");

and

intent =  new Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

The problem is that all this show a dialog where I can pick again from all apps that have images. So it shows a dialog with "Gallery", "Fotos", etc....

Is there a possibility to open just one app, without having to chose again?

user650708
  • 55
  • 4
  • Use `Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_GALLERY)` -- see [the documentation](https://developer.android.com/reference/android/content/Intent#makeMainSelectorActivity(java.lang.String,%20java.lang.String)) for more. – CommonsWare Oct 26 '21 at 20:48
  • Thank you, but unfortunately when I try it, on the smartphone is shown : "No apps can perform this action" – user650708 Oct 26 '21 at 20:58
  • Then no apps on your phone advertise themselves as a gallery app via `CATEGORY_APP_GALLERY`. "I want to just open the gallery app and not other apps with like fotos" -- you have no way of distinguishing "gallery app" unless the app declares itself as a gallery app. There are billions of phones across tens of thousands of device models, with hundreds (if not thousands) of possible "gallery" apps, both pre-installed and user-installed. – CommonsWare Oct 26 '21 at 21:16

1 Answers1

0

EDIT: If you want to use the gallery API, which does show photos from all photo apps on the phone and which is not a standalone user-accessible app itself, you could use this code:

   private static final int PICK_IMAGE = 100;
   private ImageView imageView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_image_picker);

      imageView = (ImageView) findViewById(R.id.image_view);

      Button pickImageButton = (Button) findViewById(R.id.pick_button);
      pickImageButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            openGallery();
         }
      });
   }

   private void openGallery() {
      Intent gallery = 
         new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
      startActivityForResult(gallery, PICK_IMAGE);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
         Uri imageUri = data.getData();
         imageView.setImageURI(imageUri);
      }
   }
}

Unfortunately, I don't believe choosing photos from a specific app is possible, as "the gallery app" that comes installed on devices varies by manufacturer and OS skin. For example, the default gallery on Pixel devices is actually Google Photos.

If I misunderstood, and you do want to simply launch the Gallery app, you can include the following code in your /src/[ActivityName].java file:

package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      final LinearLayout parent = findViewById(R.id.parent);
      textView = findViewById(R.id.text);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.sec.android.gallery3d");
            if (launchIntent != null) {
               startActivity(launchIntent);
            } else {
               Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

Notice that the app being opened is Gallery, the default Samsung photo gallery app. If you want to replace this with Google Photos, you would replace com.sec.android.gallery3d with com.google.android.apps.photos.

ETL
  • 188
  • 10
  • That choice is to the user. Not your app or any app. – blackapps Oct 26 '21 at 21:05
  • Thank you for your answer. Yes, I want to let the user pick an image :( – user650708 Oct 26 '21 at 21:09
  • @blackapps yes, I want to let the user choose. But I have implemented an own dialog picker for choosing between camera, gallery and fotos. Because of not being able to implement the picking from gallery or fotos, I let the user choose between camera and upload, where after picking "upload" the user has to choose again between gallery and fotos. It is just not that userfriendly that the user has to choose two times – user650708 Oct 26 '21 at 21:14
  • @ETL is there a possibility to let the user pick an image from the default photo app? – user650708 Oct 26 '21 at 21:18
  • @user650708 According to this answer, possibly, but I haven't tried it with the default gallery app specifically: https://stackoverflow.com/questions/8626421/get-preferred-default-app-on-android – ETL Oct 26 '21 at 21:24