如果该文件存在,您将如何修复崩溃并显示进度条以及停止下载所有时间而不是从内部存储读取? 我尝试使用调试器,但这无济于事
<g transform="translate(17.000000, 25.000000) rotate(${heading} 11 25)">
上面的错误也被抛出,我一直在看行,但我无法确定为什么抛出null异常 请帮忙
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.setProgress(int)' on a null object reference
at heakeme.williams2.HeaDown$DownloadFilesTask.onProgressUpdate(HeaDown.java:169)
at heakeme.williams2.HeaDown$DownloadFilesTask.onProgressUpdate(HeaDown.java:78)
答案 0 :(得分:0)
崩溃的原因是因为永远不会调用onCreateDialog
来初始化进度对话框。所以当你的代码到达这一行时:
progressdialog.setProgress(k);
progressdialog
对象为null并崩溃。你也有onCreateDialog
错误的地方,它不应该在DownloadFilesTask
内。如果有的话,它应该在你的主要片段(HeaDown
)类中。即使这样,如果您扩展DialogFragment
而不是Fragment
,也只能覆盖该方法。
我认为解决此问题的最佳方法是在不使用ProgressDialog
便利的情况下手动创建onCreateDialog
。在主HeaDown
类中创建这样的方法:
private void showProgressDialog() {
progressdialog = new ProgressDialog(getActivity());
progressdialog.setMessage("Downloading Image...");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
}
通过拨打getActivity().showDialog(Progress_download)
来取代showProgressDialog()
,您应该好好去。
答案 1 :(得分:0)
所以我做了你说的我有点工作但它仍然抛出一个文件异常错误,说它无法找到指定的路径。
E / BitmapFactory:无法解码流:java.io.FileNotFoundException:/data/user/0/heakeme.williams2/files/DownLoad/ns70.jpg(没有这样的文件或目录)
heakeme.williams2; // HEAKEME WILLIAMS N01126779
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class HeaDown extends Fragment {
URL url1;
String[] fileName;
DownloadFilesTask dw;
ImageView dwn_image;
View view;
ProgressDialog progressdialog;
int k = 0;
public static final int Progress_download = 0;
public HeaDown() {
// Required empty public constructor
}
private Dialog showProgressDialog(int progress) {
switch (progress) {
case Progress_download:
progressdialog = new ProgressDialog(getActivity());
progressdialog.setMessage("Downloading Image...");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setCancelable(false);
progressdialog.show();
default:
return null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.hea_down, container, false);
TextView tv2name = (TextView) view.findViewById(R.id.tv2name);
Button btndownload = (Button) view.findViewById(R.id.btn_download);
btndownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dw = new DownloadFilesTask();
dw.execute(url1);
}
});
dwn_image = (ImageView) view.findViewById(R.id.dl_image);
try {
url1 = new URL("http://www.ninasimone.com/wp-content/uploads/2010/09/ns70.jpg");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return view;
}
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog(Progress_download);
//getActivity().showDialog(Progress_download);
}
@Override
protected Long doInBackground(URL... urls) {
int count = urls.length;
fileName = new String[count];
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += DownloadFile(urls[i], i);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
private long DownloadFile(URL url, int i) {
// TODO Auto-generated method stub
int total = 0;
try {
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
HttpURLConnection httpConn = (HttpURLConnection) conn;
//httpConn.setAllowUserInteraction(false);
//httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
int response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
InputStream input = httpConn.getInputStream();
// download the file
// each portion will stored into an index
String[] path = url.getPath().split("/");
// we are interested only in the last index
String imageName = path[path.length - 1];
String PATH = getActivity().getApplicationContext().getFilesDir() + "/DownLoad/" ;
File folder = new File(PATH);
folder.mkdirs();
fileName[i] = folder + "/" + imageName;
OutputStream output = new FileOutputStream(folder+"/"+fileName);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return total;
}
protected void onPostExecute(Long result) {
getActivity().removeDialog(Progress_download);
Toast.makeText(getActivity(), "Downloaded " + result + " bytes", Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(fileName[0]);
dwn_image.setImageBitmap(myBitmap);
}
protected void onProgressUpdate(Integer... values) {
Log.d("MyAsyncTask","onProgressUpdate - " + values[0]);
int i = values.length;
for (int ii = 0; ii< i;ii++){
k= k+ values[ii];
}
progressdialog.setProgress(k);
//.setText(String.valueOf(k));
}
}
}