2012-10-24 3 views
0

gridview 애플리케이션을 만들었으며 json에서 제공 한 URL이있는 이미지의 경로를 얻습니다. JSON은 이미지의 URL 경로를 많이 제공하며 모든 URL에는 잘못된 경로가 있습니다.android에서 잘못된 URL 오류를 throw하는 방법

어떻게하면 잘못된 URL을 던져서 다른 URL을 가져올 수 있습니까?

전체 코드 MainActivity. CountryJSONParser

/** AsyncTask to download and load an image in ListView */ 
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{ 

     @Override 
     protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) { 

      InputStream iStream=null; 
      String imgUrl; 
      String frameUrl; 
      imgUrl = (String) hm[0].get("photo_path"); 
      frameUrl = (String) hm[0].get("frame_path"); 
      int position = (Integer) hm[0].get("position"); 

      URL url; 
      URL urlFrame; 
      try { 
       url = new URL(imgUrl); 
       urlFrame = new URL(frameUrl); 
       // Creating an http connection to communicate with url 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

       // Connecting to url 
       urlConnection.connect(); 

       // Reading data from url 
       iStream = urlConnection.getInputStream(); 

       // Getting Caching directory 
       File cacheDirectory = getBaseContext().getCacheDir(); 

       // Temporary file to store the downloaded image 
       File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png"); 

       // The FileOutputStream to the temporary file 
       FileOutputStream fOutStream = new FileOutputStream(tmpFile); 

       // Creating a bitmap from the downloaded inputstream 

       Bitmap b = BitmapFactory.decodeStream(iStream); 

       // Writing the bitmap to the temporary file as png file 
       b.compress(Bitmap.CompressFormat.PNG,100, fOutStream); 

       // Flush the FileOutputStream 
       fOutStream.flush(); 

       //Close the FileOutputStream 
       fOutStream.close(); 

       // Create a hashmap object to store image path and its position in the listview 
       HashMap<String, Object> hmBitmap = new HashMap<String, Object>(); 

       // Storing the path to the temporary image file 
       hmBitmap.put("photo",tmpFile.getPath()); 
       hmBitmap.put("frame", tmpFile.getPath()); 

       // Storing the position of the image in the listview 
       hmBitmap.put("position",position); 


       // Returning the HashMap object containing the image path and position 
       return hmBitmap; 

      }catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
+0

은 자신의 예외'InvalidURLException' 쓰기 적절한 (그리고 아마도 그것을 잡을) – keyser

답변

0
InputStream iStream = null; 
    String imgUrl; 
    String frameUrl; 
    imgUrl = (String) hm[0].get("photo_path"); 
    frameUrl = (String) hm[0].get("frame_path"); 
    int position = (Integer) hm[0].get("position"); 

    URL url; 
    URL urlFrame; 
    try { 
     url = new URL(imgUrl); 
     urlFrame = new URL(frameUrl); 
     // Creating an http connection to communicate with url 
     HttpURLConnection urlConnection = (HttpURLConnection) url 
       .openConnection(); 

     // Connecting to url 
     urlConnection.connect(); 

     // Reading data from url 
     iStream = urlConnection.getInputStream(); 

     // Getting Caching directory 
     File cacheDirectory = getBaseContext().getCacheDir(); 

     // Temporary file to store the downloaded image 
     File tmpFile = new File(cacheDirectory.getPath() + "/wpta_" 
       + position + ".png"); 

     // The FileOutputStream to the temporary file 
     FileOutputStream fOutStream = new FileOutputStream(tmpFile); 

     // Creating a bitmap from the downloaded inputstream 

     Bitmap b = BitmapFactory.decodeStream(iStream); 

     // Writing the bitmap to the temporary file as png file 
     b.compress(Bitmap.CompressFormat.PNG, 100, fOutStream); 

     // Flush the FileOutputStream 
     fOutStream.flush(); 

     // Close the FileOutputStream 
     fOutStream.close(); 

     // Create a hashmap object to store image path and its position in 
     // the listview 
     HashMap<String, Object> hmBitmap = new HashMap<String, Object>(); 

     // Storing the path to the temporary image file 
     hmBitmap.put("photo", tmpFile.getPath()); 
     hmBitmap.put("frame", tmpFile.getPath()); 

     // Storing the position of the image in the listview 
     hmBitmap.put("position", position); 

     // Returning the HashMap object containing the image path and 
     // position 
     return hmBitmap; 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     return null; 
    } catch (FileNotFoundException e) { 
     return null; 
    } catch (IOException e) { 
     return null; 
    } 
+0

나는이 작업을 수행하려고 할 때 그것을 던져하지만 오류입니다. 내 MainActivity ImageLoaderTask 봐 많은 오류가 있습니다. 'urlConnection'' FileOutputStream' – kongkea

+0

모두를 따로 모아 두는 것이 가장 좋습니다. – Yahor10

+0

이미 분리되었지만'return null '에 오류가 하나 있습니다. 그것을 분리 할 수 ​​있습니까? – kongkea