1

我正在尝试使用带有封闭样本的 Chris Banes 的 PhotoView 库。我对 sample 进行了一些更改,以从 URL(在 Internet 上)加载图像,而不是从 drawable as sample 加载图像。这是代码:

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

问题是图像没有加载,只是返回一个空白页。当我尝试一些捏缩放动作时,奇怪的事情发生了,图像被加载并正常工作。有人有建议吗?谢谢。

4

2 回答 2

0

尝试先将图像加载到位图中,然后将位图设置为 imageview。

位图的网址:

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

于 2013-01-06T10:09:49.280 回答
-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();
于 2013-01-08T12:38:08.417 回答