我想检查为什么在单击图像按钮时会发生此 looper.prepare() 异常,并且当我按下下载图像的按钮时会发生运行时异常,并且我是 android 中学习线程的新手。请帮助删除此异常
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
EditText editText ;
ListView listView;
String [] listOfImages ;
private ProgressBar progressBar;
private LinearLayout loadingSection = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.urLList);
editText = findViewById(R.id.downloadURL);
progressBar = findViewById(R.id.downloadProgress);
listOfImages = getResources().getStringArray(R.array.imageUrLs);
listView.setOnItemClickListener(this);
}
public void downloadImage(View view){
// File file = getExternalFilesDir(Environment.DIRECTORY_DCIM);
// L.s(this,file.getAbsolutePath());
// String url = listOfImages[0];
// Uri uri = Uri.parse(url);
// L.s(this,uri.getLastPathSegment());
String url = editText.getText().toString();
Thread myThread = new Thread(new DownloadImagesThread(url));
myThread.start();
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
editText.setText(listOfImages[i]);
}
public boolean downloadImageUsingThread(String url) {
/* 1 create URL object that represent a URL
2 Open conncetion using the URL object
3 read data using a input stream into a byte Array
4 open a file output stream to save data on Sd card
5 write data to the file output stream
6 close the connection
*/
boolean successful = false;
URL downloadURL = null;
InputStream inputStream = null;
HttpURLConnection connection = null;
FileOutputStream fileOutputStream = null;
File file = null;
try {
downloadURL = new URL(url);
connection = (HttpURLConnection) downloadURL.openConnection();
inputStream = connection.getInputStream();
file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/"+Uri.parse(url).getLastPathSegment(),Environment.DIRECTORY_DCIM);
fileOutputStream = new FileOutputStream(file);
L.s(this,""+file.getAbsolutePath());
int read = -1 ;
byte [] buffer = new byte[1024];
while ((read =inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer , 0, read);
}
successful = true ;
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(connection != null){
connection.disconnect();
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return successful;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate the menu: this add items in prgress bar if it is present
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
// private class DownloadImagesThread implements Runnable{
//
// @Override
// public void run() {
// downloadImageUsingThread(listOfImages[0]);
// }
// }
此类实现线程并在用户按下按钮时用于下载 url 然后触发线程
private class DownloadImagesThread implements Runnable {
private String url ;
DownloadImagesThread (String url){
this.url = url;
}
@Override
public void run() {
downloadImageUsingThread(url);
}
}
}