Android 从服务器下载图像并保存在sd卡中

Android 从服务器下载图像并保存在sd卡中,android,Android,嗨,在我的应用程序中,我必须从服务器下载图像并保存到sd卡中。请告诉我怎么做。我用计算机解决了这个问题 private void downloadImagesToSdCard(String downloadUrl,String imageName) { try{ URL url = new URL(downloadUrl); //you can write here any link File myDir = new File("/sdcard"+"/

嗨,在我的应用程序中,我必须从服务器下载图像并保存到sd卡中。请告诉我怎么做。

我用计算机解决了这个问题

private void downloadImagesToSdCard(String downloadUrl,String imageName)
{
    try{
        URL url = new URL(downloadUrl); //you can write here any link

        File myDir =  new File("/sdcard"+"/"+Constants.imageFolder);
        //Something like ("/sdcard/file.mp3")


        if(!myDir.exists()){
            myDir.mkdir();
            Log.v("", "inside mkdir");

        }

        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = imageName;
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 

             /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();
            InputStream inputStream = null;
           HttpURLConnection httpConn = (HttpURLConnection)ucon;
          httpConn.setRequestMethod("GET");
          httpConn.connect();

          if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
           inputStream = httpConn.getInputStream();
          }

            /*
             * Define InputStreams to read from the URLConnection.
             */
           // InputStream is = ucon.getInputStream();
            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */

            FileOutputStream fos = new FileOutputStream(file);
            int size = 1024*1024;
            byte[] buf = new byte[size];
            int byteRead;
            while (((byteRead = inputStream.read(buf)) != -1)) {
                fos.write(buf, 0, byteRead);
                bytesDownloaded += byteRead;
            }
            /* Convert the Bytes read to a String. */

            fos.close();

    }catch(IOException io)
    {
        networkException = true;
        continueRestore = false;
    }
    catch(Exception e)
    {   
        continueRestore = false;
        e.printStackTrace();
    }
}

谢谢你的回复……

我用计算机解决了这个问题

private void downloadImagesToSdCard(String downloadUrl,String imageName)
{
    try{
        URL url = new URL(downloadUrl); //you can write here any link

        File myDir =  new File("/sdcard"+"/"+Constants.imageFolder);
        //Something like ("/sdcard/file.mp3")


        if(!myDir.exists()){
            myDir.mkdir();
            Log.v("", "inside mkdir");

        }

        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = imageName;
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 

             /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();
            InputStream inputStream = null;
           HttpURLConnection httpConn = (HttpURLConnection)ucon;
          httpConn.setRequestMethod("GET");
          httpConn.connect();

          if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
           inputStream = httpConn.getInputStream();
          }

            /*
             * Define InputStreams to read from the URLConnection.
             */
           // InputStream is = ucon.getInputStream();
            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).
             */

            FileOutputStream fos = new FileOutputStream(file);
            int size = 1024*1024;
            byte[] buf = new byte[size];
            int byteRead;
            while (((byteRead = inputStream.read(buf)) != -1)) {
                fos.write(buf, 0, byteRead);
                bytesDownloaded += byteRead;
            }
            /* Convert the Bytes read to a String. */

            fos.close();

    }catch(IOException io)
    {
        networkException = true;
        continueRestore = false;
    }
    catch(Exception e)
    {   
        continueRestore = false;
        e.printStackTrace();
    }
}

谢谢你的回复……

你在谷歌上搜索过这个问题吗?你试过什么?你在谷歌上搜索过这个问题吗?您尝试了什么?请提供完整的代码或引用使用的列表,使用局部变量,以便您的代码对其他人有用。请提供完整的代码或引用使用的列表,使用局部变量,以便您的代码对其他人有用。