6

I'am uing the below code to open the android default gallery app. It opens all the image folders under sdcard. How can i open only one particular folder?

    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent, "Pick any photo"), SELECT_IMAGE_FROM_GALLERY_CONSTANT);
Santhosh
  • 4,956
  • 12
  • 62
  • 90
  • There are some more answers to this question on http://stackoverflow.com/questions/13418807/how-can-i-display-images-from-a-specific-folder-on-android-gallery – ShawnFeatherly Jun 11 '15 at 22:07

1 Answers1

7

Use this following code to get a particular folder image.

import java.io.File;
import android.app.Activity;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SDCARD123Activity extends Activity implements MediaScannerConnectionClient {
    public String[] allFiles;
    private String SCAN_PATH ;
    private static final String FILE_TYPE="image/*";

    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/Photo/");
        allFiles = folder.list();
        //   uriAllFiles= new Uri[allFiles.length];          
        for(int i = 0; i < allFiles.length; i++) {
            Log.d("all file path" + i, allFiles[i]+allFiles.length);
        }
        //  Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
        SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/Photo/"+allFiles[0];
        System.out.println(" SCAN_PATH  " +SCAN_PATH);

        Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
        Button scanBtn = (Button)findViewById(R.id.scanBtn);
        scanBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                startScan();
            }});
    }

    private void startScan() {
        Log.d("Connected","success"+conn);
        if (conn!=null) {
            conn.disconnect();
        }
        conn = new MediaScannerConnection(this,this);
        conn.connect();
    }

    @Override
    public void onMediaScannerConnected() {
        Log.d("onMediaScannerConnected","success"+conn);
        conn.scanFile(SCAN_PATH, FILE_TYPE);    
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            Log.d("onScanCompleted", uri + "success" + conn);
            System.out.println("URI " + uri);             
            if (uri != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                startActivity(intent);
            }
        } finally {
            conn.disconnect();
            conn = null;
        }
    }
}
user812786
  • 4,302
  • 5
  • 38
  • 50
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • Is it possible to invoke share option in gallery from this code ? – Santhosh May 25 '12 at 10:21
  • Is there any way to invoke share option on a image belongs to my particular directory from our program? – Santhosh May 25 '12 at 10:26
  • yeah we have but.... first we need to get the image uri and should upload to the destination where you want to upload.... i mean server side.. – itsrajesh4uguys May 25 '12 at 15:36
  • Is there anyway to import all the images in the folder? Because now I can select which image I would like to see, but I want to see all the images. Thank you in advance! – Jani Bela Apr 12 '13 at 14:40
  • Hi. I get the following exception when I try to run this code: `Uncaught remote exception! (Exceptions are not yet supported across processes.) android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/2115 }` – Andranik Dec 15 '15 at 08:18