-2

My app takes a photo and saves in under Gallery/MyAppname/. I just want to open the My app's directory which is under Gallery. I just want to view all the pictures.

I want to see all the photos as same as Gallery app. Plese check the screenshot:-

enter image description here

masiboo
  • 4,537
  • 9
  • 75
  • 136
  • refer https://stackoverflow.com/questions/10749351/how-to-open-one-particular-folder-from-gallery-in-android – sasikumar Jul 27 '17 at 13:10

2 Answers2

1

You can use a MediaScannerConnectionClient implementation

package com.data.pictures;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Pictures extends Activity {
    String SCAN_PATH;
    File[] allFiles ;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_picture);

        File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/test/");
        allFiles = folder.listFiles();

        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        new SingleMediaScanner(Pictures.this, allFiles[0]);
                    }
                });
    }

    public class SingleMediaScanner implements MediaScannerConnectionClient {

        private MediaScannerConnection mMs;
        private File mFile;

        public SingleMediaScanner(Context context, File f) {
            mFile = f;
            mMs = new MediaScannerConnection(context, this);
            mMs.connect();
        }

        public void onMediaScannerConnected() {
            mMs.scanFile(mFile.getAbsolutePath(), null);
        }

        public void onScanCompleted(String path, Uri uri) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);
            mMs.disconnect();
        }

    }
}

For updated question i would suggest use this link to understand Link

Developer
  • 6,292
  • 19
  • 55
  • 115
  • it shows one pic in full screen and I have to scroll to view the next picture. I want to show all pictures in grid view just as same as shown in the gallery app. Please check the edited question for more details! – masiboo Jul 27 '17 at 13:59
  • I implemented my own gridview. But I get frequently out of memory if I have lots of photos to load. Also it is redundant to implement complete Gallery features where Android already providing one gallery. – masiboo Jul 27 '17 at 23:12
0

Following code opens the specific directory at Gallary. This code works fine for both older and new Android devices. I tested up to Android 6.

    public static void openGallery(Context context) {
        String bucketId = "";
        final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
        final Cursor cur = context.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("Your_dir_name")) {
                bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
                break;
            }
        }
        Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        if (bucketId.length() > 0) {
            mediaUri = mediaUri.buildUpon()
                    .authority("media")
                    .appendQueryParameter("bucketId", bucketId)
                    .build();
        }
        if(cur != null){
            cur.close();
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
        context.startActivity(intent);
    }
masiboo
  • 4,537
  • 9
  • 75
  • 136