2012-02-24 47 views
1

我有一個應用程序使用畫布功能,我想保存和檢索所有圖像(無論我保存)。問題保存和檢索android中的圖像?

發出

我可以保存和檢索單個圖像只

動態我保存圖像,(image001,image002 ......)

更新

try { 
       toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/image00"+saveimageid+".jpg"))); 
       saveimageid++; 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

我從image001中檢索圖像。

File imgFile = new File("/mnt/sdcard/image001.jpg"); 
if(imgFile.exists()){ 

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

    ImageView myImage = (ImageView) findViewById(R.id.ImageView01); 
    myImage.setImageBitmap(myBitmap); 

回答

1

我猜你想加載所有匹配imageXYZ.jpg的文件。

我不知道爲什麼你保存文件,如「00」 + saveimageid +」。JPG。 如果你想確保文件名是一樣長(或至少三個位置),這樣做

new File(String.format("/mnt/sdcard/image%03d.jpg", saveimageId)) 

檢索的文件可能會

File dir = new File("/mnt/sdcard/"); 
File[] images = dir.listFiles(new FilenameFilter() { 
    @Override 
    public boolean accept(final File dir, final String filename) { 
     return filename.matches("image[0-9]+\\.jpg"); 
    } 
}); 

然後你就可以使用,無論在images陣列請您

一點題外話:如果T他是Android,你不應該認爲外部存儲被放置在/ mnt/sdcard中。使用Environment.getExternalStorageDirectory()

+0

我已更新我的代碼,通過使用此代碼我的圖像未保存,我無法檢索 – Mercy 2012-02-24 11:36:09

+0

爲什麼更新您的代碼?這不是如何工作。這使我的回答與問題無關。如果你更新你還應該保留原來的代碼。 你的問題是什麼?您的評論沒有描述問題。您應該有一個包含> 0個可供您使用的文件的數組。 – 2012-02-24 11:42:31

+0

其實我的問題是我想隨機保存圖像,我想檢索圖像。通過使用此代碼我無法檢索圖像並正確保存圖像。kindly幫助我。我會更新我的原始代碼 – Mercy 2012-02-24 11:46:03