无法将图像从网址存储到SD卡

时间:2011-08-11 05:54:33

标签: android

我想将图像存储在\ mnt \ sdcard

    package com.Downld_file_frm_net;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Downld_file_frm_net extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //http://203.109.115.55/MRESC/images/150.jpg
        DownloadFromUrl("http://www.garyolsen.com/goclarke/digitalphotography/2006-2/NLechPortfolio/images/Fire%20Flower_jpg.jpg","Fire%20Flower_jpg.jpg");

    }

    public void DownloadFromUrl(String DownloadUrl, String fileName) {

           try {
                   File root = android.os.Environment.getExternalStorageDirectory();               

                   File dir = new File (root.getAbsolutePath() + "mnt/sdcard/");
                   if(dir.exists()==false) {
                        dir.mkdirs();
                   }

                   URL url = new URL(DownloadUrl); //you can write here any link
                   File file = new File(dir, fileName);

                   long startTime = System.currentTimeMillis();
                   Log.d("DownloadManager", "download begining");
                   Log.d("DownloadManager", "download url:" + url);
                   Log.d("DownloadManager", "downloaded file name:" + fileName);

                   /* Open a connection to that URL. */
                   URLConnection ucon = url.openConnection();

                   /*
                    * Define InputStreams to read from the URLConnection.
                    */
                   InputStream is = ucon.getInputStream();
                   BufferedInputStream bis = new BufferedInputStream(is);

                   /*
                    * Read bytes to the Buffer until there is nothing more to read(-1).
                    */
                   ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                   int current = 0;
                   while ((current = bis.read()) != -1) {
                      baf.append((byte) current);
                   }


                   /* Convert the Bytes read to a String. */
                   FileOutputStream fos = new FileOutputStream(file);
                   fos.write(baf.toByteArray());
                   fos.flush();
                   fos.close();
                   Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

           } catch (IOException e) {
               Log.d("DownloadManager", "Error: " + e);
           }

        }
}

权限::

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

logcate ::

   08-11 11:28:37.673: DEBUG/AndroidRuntime(7606): Shutting down VM
08-11 11:28:37.673: DEBUG/dalvikvm(7606): Debugger has detached; object registry had 1 entries
08-11 11:28:38.223: DEBUG/DownloadManager(7631): download begining
08-11 11:28:38.223: DEBUG/DownloadManager(7631): download url:http://www.garyolsen.com/goclarke/digitalphotography/2006-2/NLechPortfolio/images/Fire%20Flower_jpg.jpg
08-11 11:28:38.233: DEBUG/DownloadManager(7631): downloaded file name:Fire%20Flower_jpg.jpg
08-11 11:28:39.382: INFO/global(7631): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
08-11 11:28:42.193: DEBUG/dalvikvm(7631): GC_FOR_MALLOC freed 1181 objects / 146296 bytes in 64ms
08-11 11:28:45.552: DEBUG/DownloadManager(7631): Error: java.io.FileNotFoundException: /mnt/sdcardmnt/sdcard/Fire%20Flower_jpg.jpg (No such file or directory)
08-11 11:28:45.672: INFO/ActivityManager(70): Displayed activity com.Downld_file_frm_net/.Downld_file_frm_net: 8036 ms (total 8036 ms)
08-11 11:28:50.802: DEBUG/dalvikvm(178): GC_EXPLICIT freed 115 objects / 5224 bytes in 107ms
08-11 11:28:55.854: DEBUG/dalvikvm(190): GC_EXPLICIT freed 733 objects / 40232 bytes in 119ms

3 个答案:

答案 0 :(得分:1)

尝试替换

File dir = new File (root.getAbsolutePath() + "mnt/sdcard/");
               if(dir.exists()==false) {
                    dir.mkdirs();
               }

String myDirectoryName = "/test"
File dir = new File (root.getAbsolutePath() + myDirectoryName);
               if(dir.exists()==false) {
                    dir.mkdirs();
               }

答案 1 :(得分:1)

我认为你的文件路径不正确。当你这样做(root.getAbsolutePath()时,你会得到/ mnt / sdcard的路径,所以你不必再次追加它。

尝试以下代码。这对我有用。

File rootDirectory = Environment.getExternalStorageDirectory();
String rootPath = rootDirectory.getPath();
String filePath = rootPath + "/" + "abcd.jpg";

Shash

答案 2 :(得分:1)

试试这段代码

package com.Downld_file_frm_net;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Downld_file_frm_net extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //http://203.109.115.55/MRESC/images/150.jpg
        DownloadFromUrl("http://www.garyolsen.com/goclarke/digitalphotography/2006-2/NLechPortfolio/images/Fire%20Flower_jpg.jpg","Fire%20Flower_jpg.jpg");

    }

    public void DownloadFromUrl(String DownloadUrl, String fileName) {

           try {
                   File dir = new File("/sdcard/yourDir");               


                   if(dir.exists()==false) {
                        dir.mkdirs();
                   }

                   URL url = new URL(DownloadUrl); //you can write here any link
                   File file = new File(dir, fileName);

                   long startTime = System.currentTimeMillis();
                   Log.d("DownloadManager", "download begining");
                   Log.d("DownloadManager", "download url:" + url);
                   Log.d("DownloadManager", "downloaded file name:" + fileName);

                   /* Open a connection to that URL. */
                   URLConnection ucon = url.openConnection();

                   /*
                    * Define InputStreams to read from the URLConnection.
                    */
                   InputStream is = ucon.getInputStream();
                   BufferedInputStream bis = new BufferedInputStream(is);

                   /*
                    * Read bytes to the Buffer until there is nothing more to read(-1).
                    */
                   ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                   int current = 0;
                   while ((current = bis.read()) != -1) {
                      baf.append((byte) current);
                   }


                   /* Convert the Bytes read to a String. */
                   FileOutputStream fos = new FileOutputStream(file);
                   fos.write(baf.toByteArray());
                   fos.flush();
                   fos.close();
                   Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

           } catch (IOException e) {
               Log.d("DownloadManager", "Error: " + e);
           }

        }
}