2012-12-06 140 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