2013-01-06 2 views
1

Chris Banes의 PhotoView 라이브러리를 샘플과 함께 사용하려고합니다. drawable에서 샘플이 아닌 URL (인터넷)에서 이미지를로드하기 위해 샘플을 약간 변경했습니다. 코드는 다음과 같습니다.PhotoView에 URL 이미지를로드하는 방법

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mImageView = (ImageView) findViewById(R.id.iv_photo); 
    mCurrMatrixTv = (TextView) findViewById(R.id.tv_current_matrix); 


    //here's the method to load URL image from URL 
    new LoadImage().execute(); 
    mAttacher = new PhotoViewAttacher(mImageView); 

} 

private class LoadImage extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Simulates a background job. 

     try { 
      mImageView.setImageDrawable(grabImageFromUrl(image_url));    
     } catch (Exception e) { 
      e.getStackTrace().toString(); 
     }     
     return null; 
    } 

} 

private Drawable grabImageFromUrl(String url) throws Exception { 
     return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"); 
     } 

이미지가로드되지 않아서 빈 페이지가 반환되는 문제가있었습니다. 이상한 일은 핀치 줌 동작을 시도하고 이미지가로드되어 정상적으로 작동했을 때 발생했습니다. 누구든지 제안이 있습니까? 감사.

답변

0

이미지를 비트 맵에 먼저로드 한 다음 비트 맵을 이미지 뷰로 설정하십시오. 비트 맵

URL :

public static Bitmap getBitmapFromURL(String src) { try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap; } catch (IOException e) { e.printStackTrace(); return null; } } 

다음은 (iv.setimagebitmap 사용)

+0

안녕하세요. 감사합니다. 그러나 setimageDrawable에서 setimagebitmap으로의 전환 일뿐입니다. 문제는 여전히 존재합니다. 아니면 yr 포인트를 분명히 이해하지 못한다. 감사 –

-1
private Bitmap bmp; 
bmp = new Bitmap[1]; 

// to fetch the image 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = calculateInSampleSize(options, screenWidth, screenHeight); 
options.inJustDecodeBounds = false; 
final Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url, new Rect(), options); 


// to set the image 
Runnable action = new Runnable() { 
    public void run() { bmp = bitmap 
    } 
    }; 
runOnUiThread(action); 

이제 당신은 BMP에서 이미지를 가지고있다. 가져 가서 갤러리 용 어댑터에서 설정하십시오.

ImageView imageView = new ImageView(container.getContext()); 
PhotoViewAttacher attacher = new PhotoViewAttacher(imageView); 
imageView.setImageBitmap(bmp); 
attacher.update(); 
관련 문제