2014-09-24 4 views
1

저는 안드로이드 애플리케이션을 개발했습니다. 안드로이드 애플리케이션은 웹 페이지를 파싱하고 결과적으로 그림과 텍스트를 반환합니다. 그러나 고객에게 응용 프로그램 코드를 보여 주면 그는 더 쉬운 알고리즘을 사용하여 응용 프로그램을 작성해야한다고 말했습니다. 응용 프로그램은 picture-urls 및 text-urls를 찾은 다음 설립 된 URL을 사용하여 내용을 표시해야합니다. 그것을하는 방법?안드로이드에서 그림과 URL을 구하십시오.

이 내 응용 프로그램에서 구문 분석 클래스 :

public class NetworkConnect extends AsyncTask<String, Void, Void> 
     { 
     //Background processing 
      ProgressDialog parsingBar = ProgressDialog.show(StackParser.this, "Working...", "requesting to URL and parsing content", true, false); 
      protected Void doInBackground(String... arg) 
      { 
       try 
       { 
       Log.d(LOG_TAG, arg[0]); 
       Document doc; 
       Elements urls; 
       Elements data; 
       Intent intent = new Intent(StackParser.this, AvailableContent.class); 
       doc = Jsoup.connect(arg[0]).timeout(10000).get(); 
       data = doc.getAllElements(); 
       Log.d(LOG_TAG, "Data: " + data.toString()); 
       stringData = doc.text(); 
       Log.d(LOG_TAG, "String data: " + stringData.toString()); 
       urls = doc.select("[href$=.png]"); 
       imageURL = urls.get(0).attr("href"); 
       Log.d(LOG_TAG, "imageURL = " + imageURL); 
       if(!stringData.equals("")) 
       { 
        intent.putExtra("Send to the content-activity", stringData); 
        intent.putExtra("Send imagesURLs to the content-activity", imageURL); 
        startActivity(intent); 
        finish(); 
       } 
       else 
       { 
        intent.putExtra("Send to the content-activity", "empty"); 
        startActivity(intent); 
        finish(); 
       } 
       } 
       catch (IOException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } //catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       //e.printStackTrace(); 
      //} 
      return null; 
      } 
      protected void onPostExecute(Void result) 
      { 
       //Убираем диалог загрузки 
       parsingBar.dismiss(); 
      } 
     } 
     } 
+0

당신은에서 URL을 얻고있다 이 비동기 작업? ? –

+0

@BalvinderSingh, 아니요,이 비동기 작업은 웹 페이지를 구문 분석합니다. – pragmus

+0

Log.d (LOG_TAG, "imageURL ="+ imageURL); 무슨이 줄 인쇄 ?? –

답변

1

는이 코드를 사용

public static Drawable LoadImageFromWebOperations(String url) { 
try { 
    InputStream is = (InputStream) new URL(url).getContent(); 
    Drawable d = Drawable.createFromStream(is, "src name"); 
    return d; 
} catch (Exception e) { 
    return null; 
} 

How to display image from URL on Android

1

으로 시도 유무 :

 image.setImageBitmap(decodeSampledBitmapFromUri(url, 200, 200)); 

    public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { 

     Bitmap bm = null; 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     bm = BitmapFactory.decodeFile(path, options); 

     return bm; 
    } 

    public static int calculateInSampleSize(

     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float)height/(float)reqHeight);  
      } else { 
       inSampleSize = Math.round((float)width/(float)reqWidth);  
      } 
     } 

     return inSampleSize;  
    } 

    @Override 
    public boolean onNavigationItemSelected(int arg0, long arg1) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
관련 문제