是否有可能将class DownloadImage extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... mImageUrl) {
int count;
try {
URL url = new URL(mImageUrl[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedImage.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String mImageUrl) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedImage.jpg";
// setting downloaded into image view
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
给我的表示放入字符串变量?
喜欢这个
Packet.show()
所以我可以将它存储起来供以后使用,并在需要时打印出来?
答案 0 :(得分:1)
.show()
只需打印到stdout即可。您需要将sys.stdout
替换为可以保存书写字符串的对象。
例如,在以下示例中,io.BytesIO
用于捕获写入的字符串:
>>> import sys
>>> from io import BytesIO
>>> from scapy.all import Ether, IP, ICMP, Net
>>> packets = Ether()/IP(dst=Net("google.com/30"))/ICMP()
>>> old_stdout, sys.stdout = sys.stdout, BytesIO()
>>> try:
... packets.show()
... output = sys.stdout.getvalue() # retrieve written string
... finally:
... sys.stdout = old_stdout # Restore sys.stdout
...
>>> output[:10]
'###[ Ether'