android - 将图片从url保存到sdcard

标签 android

我正在将图片从 url 保存到 sdcard。但是 sdcard 中的图像大小为 0。图像是在 sdcard 中创建的,但现在从 url 检索数据并保存。所以它给了我 0 尺寸。

try
   {
     URL url = new URL("http://api.androidhive.info/images/sample.jpg");
      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 (new File(storagePath,username+".png"));
          try {
              byte[] buffer = new byte[2048];
              int bytesRead = 0;
              while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                  output.write(buffer, 0, bytesRead);
              }
          } finally {
              output.close();
          }
      } finally {
          input.close();
      }
     } catch(Exception e)
     {
         System.out.println("error in sd card "+e.toString());
     } 

最佳答案

试试这个代码。它有效...

您应该具有互联网权限和写入外部存储的权限。

try
{   
  URL url = new URL("Enter the URL to be downloaded");
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setRequestMethod("GET");
  urlConnection.setDoOutput(true);                   
  urlConnection.connect();                  
  File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
  String filename="downloadedFile.png";   
  Log.i("Local filename:",""+filename);
  File file = new File(SDCardRoot,filename);
  if(file.createNewFile())
  {
    file.createNewFile();
  }                 
  FileOutputStream fileOutput = new FileOutputStream(file);
  InputStream inputStream = urlConnection.getInputStream();
  int totalSize = urlConnection.getContentLength();
  int downloadedSize = 0;   
  byte[] buffer = new byte[1024];
  int bufferLength = 0;
  while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
  {                 
    fileOutput.write(buffer, 0, bufferLength);                  
    downloadedSize += bufferLength;                 
    Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
  }             
  fileOutput.close();
  if(downloadedSize==totalSize) filepath=file.getPath();    
} 
catch (MalformedURLException e) 
{
  e.printStackTrace();
} 
catch (IOException e)
{
  filepath=null;
  e.printStackTrace();
}
Log.i("filepath:"," "+filepath) ;
return filepath;

关于android - 将图片从url保存到sdcard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16414515/

相关文章:

java - 即使数据相同,Firebase 中也会存储不同类型的数据

java - 切换Activity后Fragment Activity消失

android - 使用JavaCV Android Studio的问题

android - 使用 BackBase CXP 检索联系人列表?

android - 使用 Kotlin 中的自定义布局查看 DialogFragment 中的绑定(bind)

java - 如果我在项目中使用的库已停止使用,会发生什么情况,我的应用程序会崩溃吗?

java - 登录到远程 mysql 数据库的身份验证

android - 如何使用 Android 版 Air 打开浏览器并导航到网页?

android - 默认图片库无法以编程方式在 Android HoneyComb 平板电脑 (v 3.2) 中打开

java - 如何在 Java 中格式化来自对象的 Double?