Android While语句导致代码无法执行
我设置的while语句导致它下面的代码无法执行 当我在这里使用此代码时:Android While语句导致代码无法执行,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我设置的while语句导致它下面的代码无法执行 当我在这里使用此代码时: while ((count = is.read(data)) != -1) { total += count; publishProgress("" + (int) ((total * 100) / lenghtOfFile)); } 要更新文件下载的进度对话框栏,我需要的其他代码无法执行 我不知道为什么会这样,如果有人能解释和帮助,那就太好了 完整代码
while ((count = is.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
}
要更新文件下载的进度对话框栏,我需要的其他代码无法执行
我不知道为什么会这样,如果有人能解释和帮助,那就太好了
完整代码:
public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL mImageUrl;
String myFileUrl1;
Bitmap bmImg = null;
public SetWallpaperAsync(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setMessage("Downloading Image...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
pDialog.setMax(100);
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
InputStream is = null;
int count;
try {
mImageUrl = new URL(args[0]);
// myFileUrl1 = args[0];
HttpURLConnection conn = (HttpURLConnection) mImageUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
int lenghtOfFile = conn.getContentLength();
byte data[] = new byte[1024];
long total = 0;
while ((count = is.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
}
//This code doesn't get executed when using the code above
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
bmImg = BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
return null;
}
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String args) {
// TODO Auto-generated method stub
if (bmImg == null) {
Toast.makeText(context, "Image still loading...",
Toast.LENGTH_SHORT).show();
pDialog.dismiss();
}
else {
WallpaperManager wpm = WallpaperManager.getInstance(context);
try {
wpm.setBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pDialog.dismiss();
Toast.makeText(context, "Wallpaper Successfully Set!",
Toast.LENGTH_SHORT).show();
}
}
}
公共类setwallparasync扩展异步任务{
私人语境;
私人对话;
字符串图像链接;
URL-mImageUrl;
字符串myFileUrl1;
位图bmImg=null;
公共setwallparasync(上下文){
this.context=上下文;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
pDialog=新建进度对话框(上下文);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
setMessage(“下载图像…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
pDialog.setMax(100);
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
InputStream=null;
整数计数;
试一试{
mImageUrl=新URL(参数[0]);
//myFileUrl1=args[0];
HttpURLConnection conn=(HttpURLConnection)mImageUrl
.openConnection();
conn.setDoInput(真);
连接();
is=conn.getInputStream();
int lenghtOfFile=conn.getContentLength();
字节数据[]=新字节[1024];
长总计=0;
而((计数=is.read(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
}
//使用上述代码时不会执行此代码
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Config.RGB_565;
bmImg=BitmapFactory.decodeStream(is,null,options);
}捕获(IOE异常){
e、 printStackTrace();
}
最后{
如果(is!=null){
试一试{
is.close();
}捕获(例外e){
}
}
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@凌驾
受保护的void onPostExecute(字符串参数){
//TODO自动生成的方法存根
如果(bmImg==null){
Toast.makeText(上下文,“图像仍在加载…”,
吐司。长度(短)。show();
pDialog.disclose();
}
否则{
WallpareManager wpm=wallpareManager.getInstance(上下文);
试一试{
wpm.setBitmap(bmImg);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
pDialog.disclose();
Toast.makeText(上下文,“墙纸成功设置!”,
吐司。长度(短)。show();
}
}
}
您需要将最大进度设置为内容长度
protected String doInBackground(String... args) {
..........
final int contentLength = conn.getContentLength();
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.setMax(contentLength);
}
});
.........
}
循环中可能存在一些异常:
while ((count = is.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
}
这导致代码的其余部分没有被执行
检查
catch语句
是否存在一些异常,或对代码流进行调试,以确定实际问题。您的,而循环消耗所有输入,即直到到达-1
流结束
当您尝试在此处解码来自同一流的位图时:
bmImg = BitmapFactory.decodeStream(is, null, options);
。。。没有可解码的内容。没有捕捉到它-什么是publishProgress()?@Jack为什么需要读取流中的字节,因为您没有将字节分配给任何其他字节?您可以直接获取InputStream并将该流传递给BitmapFactory……他的问题具有误导性。我也认为他的抱怨是没有显示进度更新,但在阅读他的代码时,我发现他评论说,问题是while循环之后的内容没有运行——我假设bmImg
从未从他的抱怨中设置。@mah让我们看看Jack对这个答案的回答。。。实际上,他正在while循环中调用publishProgress()。当while循环第一次执行时,这里的进度将为1024…while循环正在运行。当使用while代码时,由于某些奇怪的原因,它下面的代码不会被执行。请确保您的URL正在返回响应。你的循环是正确的。while((count=is.read(data))!=-1)或while((count=is.read(data))>0)都可以正常工作。@Jack为什么需要读取流的字节,因为您没有将字节分配给任何其他字节?您可以直接获取InputStream并将该流传递给BitmapFactory…克隆输入流是一个好主意还是不好?或者还有其他方法吗?@Jack使用进度感知输入流,即对流类进行子类化,并覆盖read
以发布进度通知。