1. 程式人生 > >android開啟指定目錄檔案中的所有圖片,只用於瀏覽

android開啟指定目錄檔案中的所有圖片,只用於瀏覽

使用MediaScannerConnectionClient。
在sdcard目錄下新建資料夾photo,.並在此資料夾中複製少量圖片。
public class SDCARD123Activity extends Activity implements MediaScannerConnectionClient{
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE="image/*";
private MediaScannerConnection conn;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File folder = new File("/sdcard/Photo/");
    allFiles = folder.list();        

    SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/Photo/"+allFiles[0];

    Button scanBtn = (Button)findViewById(R.id.scanBtn);
    scanBtn.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        startScan();
    }});
    }
    private void startScan()
    {
    if(conn!=null)
    {
    conn.disconnect();
    }
    conn = new MediaScannerConnection(this,this);
    conn.connect();
    }
@Override
public void onMediaScannerConnected() {
    conn.scanFile(SCAN_PATH, FILE_TYPE);    
}
@Override
public void onScanCompleted(String path, Uri uri) {
    try {
        if (uri != null) 
        {
        Intent intent = new Intent(Intent.ACTION_VIEW);//改成Intent.ACTION_PICK的話,就是正常的開啟所有圖片的相簿
        intent.setData(uri);
        startActivity(intent);
        }
        } finally 
        {
        conn.disconnect();
        conn = null;
        }
       }
}
連結:http://stackoverflow.com/questions/10749351/how-to-open-one-particular-folder-from-gallery-in-android