如果URL存在,我怎样才能获得新图像。
这是我的活动代码:
package com.ddd.funnic.TouchView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView.ScaleType;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import com.ddd.R;
import com.ddd.TouchView.InputStreamWrapper.InputStreamProgressListener;
public class UrlTouchImageView extends RelativeLayout {
protected ProgressBar mProgressBar;
protected TouchImageView mImageView = null;
protected Context mContext;
public UrlTouchImageView(Context ctx)
{
super(ctx);
mContext = ctx;
init();
}
public UrlTouchImageView(Context ctx, AttributeSet attrs)
{
super(ctx, attrs);
mContext = ctx;
init();
}
public TouchImageView getImageView() { return mImageView; }
@SuppressWarnings("deprecation")
protected void init() {
mImageView = new TouchImageView(mContext);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mImageView.setLayoutParams(params);
this.addView(mImageView);
mImageView.setVisibility(GONE);
mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.setMargins(30, 0, 30, 0);
mProgressBar.setLayoutParams(params);
mProgressBar.setIndeterminate(false);
mProgressBar.setMax(100);
this.addView(mProgressBar);
}
public void setUrl(String imageUrl)
{
new ImageLoadTask().execute(imageUrl);
}
//No caching load
public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
{
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int totalLen = conn.getContentLength();
InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen);
bis.setProgressListener(new InputStreamProgressListener()
{
@Override
public void onProgress(float progressValue, long bytesLoaded,
long bytesTotal)
{
publishProgress((int)(progressValue * 100));
}
});
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
@Override
protected void onPostExecute (Bitmap bitmap) {
if (bitmap == null)
{
mImageView.setScaleType(ScaleType.CENTER);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo);
mImageView.setImageBitmap(bitmap);
}
if(bitmap != null)
{
mImageView.setScaleType(ScaleType.MATRIX);
mImageView.setImageBitmap(bitmap);
}
mImageView.setVisibility(VISIBLE);
mProgressBar.setVisibility(GONE);
}
@Override
protected void onProgressUpdate(Integer... values)
{
mProgressBar.setProgress(values[0]);
}
}
}
如果 Bitmap == null ,我想要一个新的Url来加载另一个图片。
请帮助我,这非常重要。
答案 0 :(得分:0)
在ImageLoadTask
中,您打开URLConnection
。如果该连接不存在,将抛出异常并执行catch
子句中的代码。您可以return
catch
子句中的public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
{
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
Bitmap bm = null;
try {
...
...
} catch (Exception e) {
e.printStackTrace();
// add this line
bm = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo); // <---------------
}
return bm;
}
...
...
}
默认位图。
{{1}}