我想使用默認的Android畫廊應用程序在我的程序中打開特定文件夾的圖像。我已經使用在後的PIYUSH米什拉但問題給出這個代碼我寫的代碼如何使用Android默認畫廊打開特定文件夾圖像
public class GalleryActivity extends Activity implements MediaScannerConnectionClient{
/** Called when the activity is first created. */
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "images/*";
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/images/");
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()+"/images/"+allFiles[0];
Log.e("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.toString() + "success"+conn);
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_DEFAULT);
intent.setData(uri);
startActivity(intent);
}
} finally
{
conn.disconnect();
conn = null;
}
}
}
以下,但這段代碼也顯示了當前設備上的其他圖像
使用此ü需要發送braodcast到系統更新畫廊,否則你將無法成功,我試着這個代碼和它的工作非常好。 – PiyushMishra