AsyncTask
没有代码吗???
没有代码吗???
2016-01-29
//MainActivity
package com.example.administrator.task_test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
public static String url_temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImage(View v)
{
editText= (EditText) findViewById(R.id.editText);
url_temp=editText.getText().toString();
Log.i("sasaki", "loadImage");
Intent intent=new Intent(MainActivity.this,ImageTest.class);
startActivity(intent);
}
}
//ImageTest
package com.example.administrator.task_test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by Administrator on 2016/1/30 0030.
*/
public class ImageTest extends Activity{
ImageView imageView;
ProgressBar progressBar;
static String URL=MainActivity.url_temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagetest);
Log.i("url传输", URL);
imageView= (ImageView) findViewById(R.id.imageView);
progressBar= (ProgressBar) findViewById(R.id.progressBar);
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(URL);
}
class MyAsyncTask extends AsyncTask<String,Void,Bitmap>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("sasaki", "onPreExecute");
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
Log.i("sasaki", "onPostExecute");
progressBar.setVisibility(View.GONE);
imageView.setImageBitmap(bitmap);
}
@Override
protected Bitmap doInBackground(String... params) {
Log.i("sasaki","doInBackground");
String url=params[0];
URLConnection connection;
InputStream is;
BufferedInputStream bis;
Bitmap bitmap=null;
try {
connection=new URL(url).openConnection();
is=connection.getInputStream();
bis=new BufferedInputStream(is);
bitmap= BitmapFactory.decodeStream(bis);
is.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
}
举报