0

AsynckTask을 사용하여 이미지를 다운로드하려고하지만 이미지를 제공하지 않습니다. 내 ProgressBar은 애플리케이션을 계속 유지하면서 이미지를로드하지 않는 한 진행 상황을 보여줍니다. 이미지 Android에서 IntentService를 사용하여 오류가 발생했습니다.

public class MyIntentServiceActivity extends IntentService { 
    public static final int DOWNLOAD_ERROR=10; 
    public static final int DOWNLOAD_SUCCESS=11; 
    int byteCount=0; 
    public MyIntentServiceActivity(){ 
     super(MyIntentServiceActivity.class.getName()); 

    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     String path=intent.getStringExtra("url"); 
     final ResultReceiver receiver=intent.getParcelableExtra("receiver"); 
     Bundle bundle=new Bundle(); 
     File internal_root= Environment.getExternalStorageDirectory(); 
     //File new_folder=new File("sdcard0/IntentService_Example"); 
     //if (!new_folder.exists()){ 
      // new_folder.mkdir(); 
     // } 
     File new_file=new File(internal_root,"download_image.jpg"); 
     try { 
      URL url=new URL(path); 
      HttpURLConnection connection=(HttpURLConnection)url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setDoOutput(true); 
      connection.connect(); 
      int responseCode=connection.getResponseCode(); 
      if (responseCode!=200) 
       throw new Exception("Error in connection"); 
      InputStream is=connection.getInputStream(); 
      OutputStream fos=new FileOutputStream(new_file); 
      byte[] buffer=new byte[1024]; 
      int count=0; 
      while ((count=is.read(buffer))>0){ 
       fos.write(buffer,0,count); 
      } 
      fos.close(); 
      String file_path=new_file.getPath(); 
      bundle.putString("file_path",file_path); 
      receiver.send(DOWNLOAD_SUCCESS,bundle); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

어디에 내 실수입니다 ...

public class MainActivity extends AppCompatActivity { 
    ImageView img; 
    Button btn; 
    EditText edt; 
    ProgressBar prb; 
    SampleResultReceiver sampleResultReceiver; 
    String defalut_url="http://9xmobi.com/image/15580/size/128x128/KAJOL(12)%5B9xmobi.com%5D.jpg"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     img=(ImageView)findViewById(R.id.image_view); 
     setContentView(R.layout.activity_main); 
     edt=(EditText)findViewById(R.id.urlid); 
     btn=(Button)findViewById(R.id.button); 
     prb=(ProgressBar)findViewById(R.id.progress); 
    } 
    public void click(View view){ 
     Intent intent=new Intent(MainActivity.this,MyIntentServiceActivity.class); 
     intent.putExtra("receiver",sampleResultReceiver); 
     intent.putExtra("url", TextUtils.isEmpty(edt.getText())?defalut_url:edt.getText().toString()); 
     startService(intent); 
     prb.setVisibility(View.VISIBLE); 
     prb.setIndeterminate(true); 
    } 
    class SampleResultReceiver extends ResultReceiver { 
     /** 
     * Create a new ResultReceive to receive results. Your 
     * {@link #onReceiveResult} method will be called from the thread running 
     * <var>handler</var> if given, or from an arbitrary thread if null. 
     * 
     * @param handler 
     */ 
     public SampleResultReceiver(Handler handler) { 
      super(handler); 
     } 

     @Override 
     protected void onReceiveResult(int resultCode, Bundle resultData) { 
      switch (resultCode){ 
       case MyIntentServiceActivity.DOWNLOAD_ERROR: 
        Toast.makeText(getApplicationContext(), "error in download", 
          Toast.LENGTH_SHORT).show(); 
        prb.setVisibility(View.INVISIBLE); 
        break; 
       case MyIntentServiceActivity.DOWNLOAD_SUCCESS: 
        String file_path=resultData.getString("file_path"); 
        Bitmap bmp= BitmapFactory.decodeFile(file_path); 
       if (img!=null&&bmp!=null){ 
        img.setImageBitmap(bmp); 
        Toast.makeText(getApplicationContext(), 
          "image download via IntentService is done", 
          Toast.LENGTH_SHORT).show(); 
       } 
       else{ 
        Toast.makeText(getApplicationContext(), 
          "error in decoding downloaded file", 
          Toast.LENGTH_SHORT).show(); 
       } 
        prb.setIndeterminate(false); 
        prb.setVisibility(View.INVISIBLE); 

        break; 

      } 
      super.onReceiveResult(resultCode, resultData); 
     } 
    } 
} 

그리고 내 서비스 클래스는 다음과 같습니다

enter image description here

MainActivity :

내 응용 프로그램의 UI 디자인이다 이리? 저를 올바른 방향으로 가르쳐주십시오.

답변

0

샘플 결과 수신기 인스턴스를 사용자의 서비스 클래스로 보내면 샘플 결과 수신기 클래스의 오브젝트를 보내지 않을 때 서비스 클래스에 널 포인터를 보냅니다.

이 작업을 시도해야합니다.

public void click(View view){ 
sampleResultReceiver = new SampleResultReceiver(); 
     Intent intent=new Intent(MainActivity.this,MyIntentServiceActivity.class); 
     intent.putParceleableExtra("receiver",sampleResultReceiver); 
     intent.putExtra("url", TextUtils.isEmpty(edt.getText())?defalut_url:edt.getText().toString()); 
     startService(intent); 
     prb.setVisibility(View.VISIBLE); 
     prb.setIndeterminate(true); 
    } 
+0

이제 이미지가 메모리에 있지만 그것은 당신이 다시 활동에 이미지와 함께로드 된 파일 경로를 보내는 내가 활동에서 파일을 다시 얻을 제안하기 때문에이 번 – sreeku24

+0

을 확인하시기 바랍니다 이미지보기에로드하지 bimap 팩토리 옵션을 사용하여 비트 맵을 생성 한 다음 이미지보기를 가져온 비트 맵으로 설정할 수 있습니다. –

관련 문제