2012-12-06 132 views
0

我想創建一個文件夾並將圖像存儲到手機的內部存儲器中。我嘗試使用下面的代碼從url下載圖片。它設法將圖像加載到imageView中,但無法在內部存儲中存儲和創建文件夾。另外我沒有任何警告或錯誤消息。任何想法下面有什麼錯誤的代碼?無法將圖像存儲到內部存儲目錄中

 Bitmap bm = null; 
     InputStream in; 

      try{ 

       in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream(); 
       bm = BitmapFactory.decodeStream(new PatchInputStream(in)); 
       File mydir = this.getDir("mydir", Context.MODE_PRIVATE); 
       mydir.mkdirs(); 
       File fileWithinMyDir = new File(mydir, "myfile"); 
       FileOutputStream out = new FileOutputStream(fileWithinMyDir); 
       bm.compress(Bitmap.CompressFormat.JPEG, 85, out); 



      } 
      catch(Exception e1){ 
       e1.printStackTrace(); 
      } 
    ImageView img = (ImageView) findViewById(R.id.image_display); 
    img.setImageBitmap(bm); 
+0

的另一個問題是,如果我在內部存儲創建將它出現在畫廊中呢? –

回答

1

一旦你有你的位圖bm

FileOutputStream fos; 

try { 
    fos = openFileOutput("file_Name", Context.MODE_PRIVATE); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, fos); 
    fos.close(); 
}catch (Exception e){ 
... 
} 

將您的文件保存到內部存儲

1

使用下列內容:

String path=Environment.getExternalStorageDirectory() 
           .toString() + File.separator 

拿到目錄並保存圖像。

+0

我試過用這個。它仍然不起作用。不知道爲什麼或如何。 –

+0

這也讓我失望了。當某些或只有沒有外部存儲的Nexus設備沒有該目錄時,getExternalStorageDirectory()將返回類似/ storage/emulated/0/...的內容。它應該是/ storage/emulated/legacy/...!爲什麼在我的Nexus10上它會返回一條不存在且不正確的路徑?哎呀... – garlicman