0

다음 AsyncTask를 고려하십시오. URL에서 이미지를 다운로드하여 장치에 저장하고 수평 ProgressBar를 통해 진행 상황을 표시합니다. 지금은 변수 "비트 맵"을 전혀 사용하지 않고 있습니다 (null로 남음). 거기에 내가 할 수 원하는 두 가지이다 : 내 장치에 실제 파일을 저장하지 않고Android - 진행 상황을 표시하면서 URL에서 ImageView로 이미지로드 (이미지 저장없이)

  1. 로드 URL에서 이미지를 이미지 뷰에은 (?이 경우에도 가능) 및 ProgressBar를 그렇게하는 동안 보여 - 실제로 은 진행을 표시하며 단순한 애니메이션 서클 이상입니다.

  2. (피카소에 대해 알고있는 분에게) 피카소 (http://square.github.io/picasso/)를 사용하여 똑같은 일을하십시오. Picasso는 모든 것을 쉽게 처리 할 수 ​​있으며이 코드 줄을 사용할 수 있습니다. Picasso.with(getApplicationContext()).load(url).into(imageView); 실제로 진행 상황을 표시 할 수 있으면 좋겠지 만 (진행 상황을 실제로 보여주는 ProgressBar를 사용하여 다시 한 번) 좋을 것입니다.

어떤 도움을 주시면 감사하겠습니다. 또한 현재 코드와 개선 방법에 대한 의견을 보내 주시면 감사하겠습니다. (대부분이 YouTube 자습서가 뒤따 랐기 때문에 크레딧을 제공해야한다고 생각합니다 : https://www.youtube.com/watch?v=5HDr9FdGIVg).

private class ImageDownloadTask extends AsyncTask<String, Integer, Bitmap>{ 
    private int contentLength = -1; 
    private URL downloadURL = null; 
    private HttpURLConnection connection = null; 
    private InputStream inputStream = null; 
    private File file; 
    private OutputStream outputStream = null; 
    private int counter = 0; 
    private Bitmap bitmap = null; 

    @Override 
    protected void onPreExecute() 
    { 
     setProgressBarProgress(0); 
     showProgressBar(); 
    } 

    @Override 
    protected Bitmap doInBackground(String[] objects) 
    { 
     try 
     { 
      downloadURL = new URL(url); 
      connection = (HttpURLConnection)downloadURL.openConnection(); 
      contentLength = connection.getContentLength(); 
      inputStream = connection.getInputStream(); 
      file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + Uri.parse(objects[0]).getLastPathSegment()); 
      outputStream = new FileOutputStream(file); 

      byte buffer[] = new byte[1024]; 
      int read = -1; 

      while ((read = inputStream.read(buffer)) != -1) 
      { 
       outputStream.write(buffer, 0, read); 
       counter += read; 
       publishProgress(counter); 
      }; 
     } 
     catch (SecurityException e) 
     { 
      Msg.log("Security Exception: " + e.getMessage()); 
     } 
     catch (Exception e) 
     { 
      Msg.log("Other Exception: " + e.getMessage()); 
     } 
     finally 
     { 
      //Even if our attempt to download the image did not succeed, 
      //we should still close the connection, and the streams. 
      //Otherwise, we are potentially wasting the device's resources. 
      if (connection!=null) 
       connection.disconnect(); 

      try {inputStream.close();} 
      catch (IOException e) {e.printStackTrace();} 

      try {outputStream.close();} 
      catch (IOException e) {e.printStackTrace();} 

      try {outputStream.flush();} 
      catch (IOException e) {e.printStackTrace();} 
     } 

     return bitmap; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) 
    { 
     int progress = (int)(((double)values[0]/contentLength)*100); 
     setProgressBarProgress(progress); 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) 
    { 
     hideProgressBar(); 
     if (result != null) { 
      imageView.setImageBitmap(result); 
     } 
    } 
} 
+0

가능한 복제 (http://stackoverflow.com/questions/32184156/how-to-add-progress-bar-to- [피카소 라이브러리에 진행 표시 줄을 추가하는 방법] picasso-library) – Chisko

답변

0

피카소는 실제로 다운로드 진행률을 제공 할 수 없지만 이미지로드가 완료되면 콜백을 줄 수 있습니다.

주로 사용되는 접근 방식은 진행률 표시 줄/원을 표시하고 다운로드가 완료되면 숨 깁니다.

here 대답하고있다

+0

첫 번째 질문은 어떻게됩니까? 이 방법으로 처리 할 수 ​​있습니까? – Cookienator

관련 문제