Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gallery with folder filter

I'm using following code to open a gallery inside of my app

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, FIND_RESULT);

Is it possible to limit a list of images to only show images taken by camera? Viewing Gallery on my 2.1 system, images are grouped so there has to be a parameter that defines to which folder it belongs.

Checking the MediaStore.Images.ImageColumns I did not a find any column that would define such thing.

Could I be wrong? Because if I could create a query to filter by folder and create my own gallery view, then my problem would be solved.

like image 330
FrEaKmAn Avatar asked Oct 25 '10 22:10

FrEaKmAn


People also ask

Can you create folders in Android Gallery?

Change & create foldersOn your Android phone, open Gallery . New folder. Enter the name of your new folder. Choose where you want your folder.

How do I filter a gallery in canvas app?

Add a slider control and filter items in the galleryOn the Insert tab, select Text, select Input Text, and rename the new control to NameFilter. Move the text control below the slider.


2 Answers

You just need to implement MediaScannerConnectionClient in your activity and after that you have to give the exact path of one of the file inside that folder name here as SCAN_PATH and it will scan all the files containing in that folder and open it inside built in gallery. So just give the name of you folder and you will get all the files inside including video. If you want to open only images change FILE_TYPE="image/*"

public class SlideShow extends Activity implements MediaScannerConnectionClient {

        public String[] allFiles;
        private String SCAN_PATH ;
        private static final String FILE_TYPE = "*/*";
        private MediaScannerConnection conn;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            File folder = new File("/sdcard/yourfoldername/");
            allFiles = folder.list();

            SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0];

            Button scanBtn = (Button) findViewById(R.id.scanBtn);
            scanBtn.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    startScan();
                }
            });
        }

        private void startScan()
        {
            if(conn!=null)
            {
                conn.disconnect();
            }

            conn = new MediaScannerConnection(this, this);
            conn.connect();
        }


        public void onMediaScannerConnected()
        {
            conn.scanFile(SCAN_PATH, FILE_TYPE);    
        }


        public void onScanCompleted(String path, Uri uri)
        {
            try
            {
                if (uri != null) 
                {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    startActivity(intent);
                }
            }
            finally 
            {
                conn.disconnect();
                conn = null;
            }
        }
    }
like image 176
PiyushMishra Avatar answered Oct 30 '22 12:10

PiyushMishra


None of the above answers are correct, including the one marked as correct.

Here's the actual correct solution:

The secret is finding the bucket/album your folder is represented as. Buckets show up after a successful MediaScan so be sure any images/videos you want to show are first scanned as demonstrated multiple times above.

Let's assume I have an indexed folder in /sdcard/myapp/myappsmediafolder:

String bucketId = "";

final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
final Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);

while (cur != null && cur.moveToNext()) {
    final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
    if (bucketName.equals("myappsmediafolder")) {
        bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
        break;
    }
}

Now that we have the bucketId for our album we can open it with a simple intent.

Filters Video files:

Uri mediaUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

Filters Image files:

Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

...

if (bucketId.length() > 0) {
    mediaUri = mediaUri.buildUpon()
            .authority("media")
            .appendQueryParameter("bucketId", bucketId)
            .build();
}

Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);

I can verify this works with the built-in Gallery app. Mileage may vary with other apps such as Google Photos.

I have yet to figure out how not to filter images/video, even though within Gallery you can select a specific Album with no filter.

I figured this out by looking at the AOSP source to the gallery app.

like image 43
ShellDude Avatar answered Oct 30 '22 12:10

ShellDude