1

Possible Duplicate:
Gallery with folder filter

My app creates pictures in a directory in sdcard and I want to open gallery for it. To do that, I simply do following:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivity(intent);  

But it shows every pictures in the device. Is it possible to show only files in the directory?

Community
  • 1
  • 1
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240

1 Answers1

1

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="images/*"

public class sdActivity 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/youfoldername/");
    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()+"/yourfoldername/"+allFiles[0];
    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);
        if (uri != null) 
        {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivity(intent);
        }
        } finally 
        {
        conn.disconnect();
        conn = null;
        }
       }
}

i already answered it on this link also Built-in gallery in specific folder.

Community
  • 1
  • 1
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57
  • It doesn't work, this line : conn = new MediaScannerConnection(this,this); is not correct, cannot cast the second argument into MediaScannerConnectionClient. – nonozor Sep 06 '13 at 12:43
  • @nonozor the second this belongs to MediaScannerConnectionClient you can apply(this,this) if you are implementing MediaScannerConnectionClient in your activity. – PiyushMishra Oct 07 '13 at 22:43
  • 1
    This just opens one of the pictures but not the gallery showing an overview of the pictures in the directory..... – Ersen Osman Feb 26 '15 at 20:05