2013-01-06 8 views
1

J'essaie d'utiliser la bibliothèque PhotoView de Chris Banes avec un échantillon inclus. J'ai fait quelques changements dans l'échantillon pour charger l'image de l'URL (sur Internet), pas de drawable comme échantillon. Voici le code:Comment charger une image d'URL dans PhotoView

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"); 
     } 

Le problème était que l'image n'était pas chargée alors, il suffit de retourner une page blanche. Et la chose étrange s'est produite quand j'ai essayé quelques actions de pincement de zoom, l'image a été chargée, et a fonctionné normalement. Quelqu'un a une suggestion? Merci.

Répondre

0

Essayez de charger l'image dans un bitmap d'abord, puis définissez le bitmap à l'imageview.

Url bitmap:

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; } } 

puis utilisez iv.setimagebitmap()

+0

Salut, merci pour votre suggestion. Cependant, c'est juste un passage de setimageDrawable à setimagebitmap. Le problème est toujours là. Ou je ne comprends pas votre point clairement, si oui, s'il vous plaît expliquez-moi plus. Merci –

-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); 

Maintenant, vous avez l'image bmp. Prenez-le et placez-le dans l'adaptateur pour votre galerie.

ImageView imageView = new ImageView(container.getContext()); 
PhotoViewAttacher attacher = new PhotoViewAttacher(imageView); 
imageView.setImageBitmap(bmp); 
attacher.update(); 
Questions connexes