如何将下载的文件存储到SD卡然后检索它?

时间:2011-05-11 09:56:50

标签: android image

在我的活动中,我正在从网址下载图片。我希望这些图像只是第一次下载。稍后当我访问此页面时,它应该从SD卡中获取图像。我怎样才能做到这一点?有人可以帮忙吗?

在清单中我已设置权限:

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

我用来下载的方法是:

public static Bitmap downloadFileFromUrl(String fileUrl){

        URL myFileUrl =null;  
        Bitmap imageBitmap = null;

        try {
            myFileUrl= new URL(fileUrl);
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection connection= (HttpURLConnection)myFileUrl.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream is = connection.getInputStream();
            imageBitmap = BitmapFactory.decodeStream(is);

            //Below two lines I just tried out for saving to sd card.
            FileOutputStream out = new FileOutputStream(fileUrl);
            imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }

        return imageBitmap;
    }

3 个答案:

答案 0 :(得分:2)

试试这个方法

public void DownloadImage(String imageUrl)      
    {
        InputStream is = null;
        if((imageUrl == null) || (imageUrl.length() == 0) || (imageUrl == " "))
        {
            System.out.println("No need to download images now");
        }
        else
        {
            System.gc();


            String[] items; 

            String ImageName = null;

            URL myFileUrl =null; 
            Bitmap bmImg = null;

            String path = IMAGE_DOWNLOAD_PATH;

            FileOutputStream outStream = null;
            File file = new File(path);

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

            File outputFile;
            BufferedOutputStream bos;

                try {   
                    myFileUrl= new URL(imageUrl.trim());

                    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
                    conn.setRequestMethod("GET");

                    conn.setDoInput(true);
                    conn.setConnectTimeout(20000);
                    conn.connect();

                    is = conn.getInputStream();


            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                 ImageName = getImageName(imageUrl);

                 try {
                     outputFile = new File(file, ImageName);

                     if(outputFile.exists())                
                     {
                         System.out.println("No need to download image it already exist");
                         outputFile.delete();
                     }
                         outputFile.createNewFile();

                         outStream = new FileOutputStream(outputFile);
                         //bos = new BufferedOutputStream(outStream);
                         BufferedInputStream bis = new BufferedInputStream(is);

                            ByteArrayBuffer baf = new ByteArrayBuffer(50);

                            int current = 0;
                            while ((current = bis.read()) != -1) 
                            {
                                baf.append((byte) current);
                            }

                            outStream.write(baf.toByteArray());
                            outStream.close();




                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
        }

    }

然后从SD卡中检索图像,

File extStore = Environment.getExternalStorageDirectory();
        String file_path = "/(folder name)/"+"(image name)".trim()+".extension".trim();

        String mypath = extStore + file_path;
        Bitmap bmp=BitmapFactory.decodeFile(mypath);
ImageView image = (ImageView) v.findViewById(R.id.image);
        image.setImageBitmap(bmp);

答案 1 :(得分:0)

你应该存储你在缓存中的内容。

或者,如果您的文件名是唯一的,您需要检查文件是否存在。

答案 2 :(得分:0)

您必须将从InputStream获取的数据写入指定的SD卡位置。