How can I display images from a specific folder on android gallery

Rafael picture Rafael · Nov 16, 2012 · Viewed 36.4k times · Source

How can I display all images from a specific folder on android gallery like, for example, whatapp does. I`m using MediaScannerConnectionClient

File folder = new File("/sdcard/myfolder/");
allFiles = folder.list();
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/myfolder/"+allFiles[0];
@Override
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;
    }
}

private void startScan() {
    if (conn != null) {
        conn.disconnect();
    }
    conn = new MediaScannerConnection(this, this);
    conn.connect();
}
    @Override
public void onMediaScannerConnected() {
    conn.scanFile(SCAN_PATH, "image/*");
}

But I`m getting a error at this point:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    startActivity(intent);

Specific here:

startActivity(intent);

Fail to get type for: content://media/external/images/media/267830 No Activity found to handle Intent

On onScanCompleted my path and uri parameters are not null.

Answer

Talha picture Talha · Nov 16, 2012

Hi you can use the code below, i hope it helps you .

package com.example.browsepicture;

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 BrowsePicture2 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()+"/aaaa/");
        allFiles = folder.listFiles();

        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        new SingleMediaScanner(BrowsePicture2.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();
        }

    }
}