2014-09-20 41 views
0

我正在getiing一个页面的xml内容,并解析从这个XML的所有图像Url的URL。 然后,我使用位图将图像显示在imageviwer中。此外,我需要该图像用于我的下一个版面设计。如何存储图像以供进一步使用,如果能够如何操作,我可以将图像存储在可绘制文件夹中。我刚才提到在下面,我需要保存的ImageView内容绘制文件夹中的代码..Inputstream无法连接到一个URL

  protected Void doInBackground(String... params) { 

        try { 
         url=new URL(UrlLink2) ; 
         urlConnection = (HttpURLConnection) url.openConnection(); 
          urlConnection.setRequestMethod("GET"); 
          InputStream isp=urlConnection.getInputStream(); 
         DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance(); 
         DocumentBuilder Db=DBF.newDocumentBuilder(); 
          doc=Db.parse(isp); 
          Element rootElem=doc.getDocumentElement(); 
          NodeList itemlist=rootElem.getElementsByTagName("item"); 
          Node currentitem=null; 
          Node childnode=null; 
          Node ContentChild=null; 
          Node CddatatChild=null; 
          NodeList childnodeList=null; 
          NodeList CddataList=null; 
          NodeList ContentChilList=null; 
          for(int i=0;i<itemlist.getLength();i++){ 
           currentitem=itemlist.item(i); 
           childnodeList=currentitem.getChildNodes(); 
           for(int j=0;j<childnodeList.getLength();j++){ 
            childnode=childnodeList.item(j); 
            if(childnode.getNodeName().equalsIgnoreCase("content:encoded")){ 
             ContentChilList=childnode.getChildNodes(); 
             ContentChilList.getLength(); 
             CddatatChild=ContentChilList.item(0); 
             CddataList=CddatatChild.getChildNodes(); 
              if(CddatatChild.getNodeName().equalsIgnoreCase("#cdata-section")){ 
               GetCddata=CddatatChild.getTextContent().toString(); 
                GetCddata=CddatatChild.getTextContent(); 
             } 
            } 
           } 
          } 
         } 
        catch(Exception e){ 

        } 
        try{ 
         int i=0;     
          String ss=GetCddata; 
       Pattern PatternImgURLS = Pattern.compile("(?<=a href=\")(.*?)(?=\")"); 
       Pattern PatternImgname = Pattern.compile("(?<=2014\\/09\\/)(.*?)(?=\\.)"); 
       Matcher Imagematcher = PatternImgURLS.matcher(ss);  
       Imagematcher.matches(); 
       while (Imagematcher.find()) { 
         ImgUrl=Imagematcher.toMatchResult().group(); 
         ImageUrls.add(ImgUrl); 
         i++;  
        } 
       for(int count=0;count<ImageUrls.size();count++){ 
        Matcher Namematcher = PatternImgname.matcher(ImageUrls.get(count)); 
        Namematcher.matches(); 
        while (Namematcher.find()) { 
        String MatchTxt=Namematcher.toMatchResult().group(); 
        String lowerMatchTxt = MatchTxt.toLowerCase(); 
        ImageName.add(lowerMatchTxt); 
        Log.v("dd",ImageName.get(count)); 
         } 
        } 
         Log.v("dd",ImageUrls.get(0)); 
         int t=ImageUrls.size();  
         Log.v("dd",toString().valueOf(t)); 
         urlConnection.disconnect(); 
         url=new URL(ImageUrls[0]) ; 
         urlConnection = (HttpURLConnection) url.openConnection(); 
         urlConnection.setRequestMethod("GET"); 
         InputStream isp2=urlConnection.getInputStream(); 
         bitmap=BitmapFactory.decodeStream(isp2); 
         urlConnection.disconnect(); 
        } 
        catch(Exception e){ 
           Log.v("h",e.toString()) ; } 

          @Override   
      protected void onPostExecute(Void result) { 
          super.onPostExecute(result); 
          dialog.dismiss(); 
Here I am displaying---> Imageview.setImageBitmap(bitmap); 
the image in Image viwer  
Now how do I store the image from the imageviewer to 
drawable folder in andriod is that possiable. 
     } 
     } 

回答

0

我建议不要自己去实现它,而是采取众所周知库,可以为你做。例如,Picasso具有适合您需要的功能。您只需通过一个URL,它和毕加索将在下次使用一个缓存文件夹中下载和保存图像:

Picasso.with(context).load(imageUrl).into(imageView); 

所有你需要做的是要分析这些图像的URL,并在内存中存储一​​些。

0

首先,你不能保存youur在水库图像/绘制因为一旦部署了.apk文件,资源就无法更改。

但是,您可以使用应用程序的缓存目录。这是一个属于您的应用程序的非公共文件夹,将在卸载时被删除。此外,如果设备的空间不足,系统可能会从此目录中删除。

context.getCacheDir() 

不过要注意的是,文件说:它可以通过访问

你不应该依赖系统中删除这些文件,你的;你应该始终有一个合理的最大值,如1 MB,为的空间,你有缓存文件占用量,并清理这些文件超过了空间

时,如果你需要一个很大的空间,并会而是使用可以拨打的SD卡

getExternalCacheDir() 

改为。这些在卸载时也会被删除,但系统不会监视外部存储空间的可用空间,因此如果空间不足,将不会自动删除这些文件。如果使用此选项,您还应该检查外部存储可与

Environment.getExternalStorageState() 

Environment.getExternalStorageState() 试图写入之前。