2013-05-28 87 views
1

我想用一个按钮在我的代码中的URL保存图像我已经创建目标文件夹,但我已经尝试过各种代码来保存图片,但不工作:从网址保存图像

public class B_X extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.bx); 

     Button buttonSetWallpaper = (Button)findViewById(R.id.bSetWall); 
     buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       File folder = new File(Environment.getExternalStorageDirectory() + "/PerfectAss/"); 
       boolean success = false; 
       if (!folder.exists()) { 
        success = folder.mkdirs(); 
       } 

       if (!success) { 
      } else { 
      } 

       Toast.makeText(getApplicationContext(), "The image has been saved", Toast.LENGTH_LONG).show(); 
        URL url = new URL ("http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg"); 
        InputStream input = url.openStream(); 
        try { 

         File storagePath = Environment.getExternalStorageDirectory(); 
         OutputStream output = new FileOutputStream (storagePath + "/myImage.png"); 
         try { 
          byte[] buffer = new byte[1500]; 
          int bytesRead = 0; 
          while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
          output.write(buffer, 0, bytesRead); 
          } 
          } finally { 
           output.close(); 
           } 
           } finally { 
           input.close(); 
           } 

请帮我解决这段代码。

+0

如果你在谷歌搜索相同的问题,你会得到100链接。 – NaserShaikh

+1

那么你真的尝试过什么?因为上面的代码没有显示任何与下载图像和保存图像有关的内容。 –

+0

我把我的感受? – Gioele

回答

5

使用这样的简单:它保存文件在"sdcard/dhaval_files/"。只需替换您的文件夹名称并在Android清单文件中授予write_external_storage权限。

public void file_download(String uRl) { 
     File direct = new File(Environment.getExternalStorageDirectory() 
       + "/dhaval_files"); 

     if (!direct.exists()) { 
      direct.mkdirs(); 
     } 

     DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE); 

     Uri downloadUri = Uri.parse(uRl); 
     DownloadManager.Request request = new DownloadManager.Request(
       downloadUri); 

     request.setAllowedNetworkTypes(
       DownloadManager.Request.NETWORK_WIFI 
         | DownloadManager.Request.NETWORK_MOBILE) 
       .setAllowedOverRoaming(false).setTitle("Demo") 
       .setDescription("Something useful. No, really.") 
       .setDestinationInExternalPublicDir("/dhaval_files", "test.jpg"); 

     mgr.enqueue(request); 

    } 
+0

非常感谢,但我把它放在了下面,但我可以把它放在public void onClick(View arg0)下? – Gioele

+0

你必须调用这个方法。不要把所有的代码放在onclick上。只是这样做︰public void onClick(View arg0){file_download(“www.xyz/mobile/img/test.jpg”)} –

+0

谢谢,但我想使用按钮保存图像,然后保存图像我必须将其用于其他活动,我该怎么做? – Gioele

0

有你有权限进入你的清单文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

并尝试这个代码

或者那就试试这个代码,它会工作

URL url = new URL ("file://some/path/anImage.png"); 
InputStream input = url.openStream(); 

try { 
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory() 
    File storagePath = Environment.getExternalStorageDirectory(); 
    OutputStream output = new FileOutputStream (storagePath + "/myImage.png"); 

    try { 
     byte[] buffer = new byte[aReasonableSize]; 
     int bytesRead = 0; 

     while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
      output.write(buffer, 0, bytesRead); 
     } 
    } finally { output.close(); } 
} finally { input.close(); } 
+0

是的,我有权限 – Gioele

+0

好吧试试上面的代码和详细信息检查此链接http://android-example-code.blogspot.in/ p/download-store-and-read-images-from.html –

+0

URL url = new URL(“http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg”); eclipse告诉我说明资源路径位置类型 未处理的异常类型MalformedURLException B_X.java – Gioele