Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将下载的文件存储到SD卡,然后检索它?_Android_Image - Fatal编程技术网

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

Android 如何将下载的文件存储到SD卡,然后检索它?,android,image,Android,Image,在我的活动中,我从url下载图像。我希望这些图片只能第一次下载。稍后当我访问此页面时,它应该从SD卡中获取图像。我该怎么做?有人能帮忙吗 在清单中,我已设置权限: 我下载的方法是: public static Bitmap downloadFileFromUrl(String fileUrl){ URL myFileUrl =null; Bitmap imageBitmap = null; try { myFile

在我的活动中,我从url下载图像。我希望这些图片只能第一次下载。稍后当我访问此页面时,它应该从SD卡中获取图像。我该怎么做?有人能帮忙吗

在清单中,我已设置权限:

我下载的方法是:

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;
    }

你应该把缓存里的东西放在某个地方


或者,如果文件名是唯一的,则必须检查文件是否存在。

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

尝试此方法

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);