1. 程式人生 > >Android -- 採用系統相簿瀏覽指定路徑下照片

Android -- 採用系統相簿瀏覽指定路徑下照片

//開啟系統相簿
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivity(intent);

//開啟指定的一張照片
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(pictureFilepath), "image/*");
startActivity(intent);

//開啟系統相簿瀏覽照片
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
startActivity(intent);

// 採用MediaScannerConnection掃描制定路徑下的圖片檔案,並啟動系統相簿進行瀏覽


自己寫的一個類

public class PictureScanner implements MediaScannerConnection.MediaScannerConnectionClient{

    private MediaScannerConnection mMs;
    private File mFile;
    private Context context;

    File[] allFiles ;

    public PictureScanner(Context context) {
        File folder = new File(pictureFolderPath);
        allFiles = folder.listFiles();
        swap(allFiles);

        this.context = context;
        mFile = allFiles[0];
        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);
        context.startActivity(intent);
        mMs.disconnect();
    }

    private void swap(File a[]){
        int len = a.length;
        for(int i=0;i<len/2;i++){
            File tmp = a[i];
            a[i] = a[len-1-i];
            a[len-1-i] = tmp;
        }
    }
}

//呼叫
new PictureScanner(MyActivity.this);