How to start gallery app from an android app?

1.1k Views Asked by At

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?

1

There are 1 best solutions below

5
ETL On

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.