我想从服务器下载图像,将其保存在 SD 卡上,然后显示。我编写了该代码,但它不起作用 - 没有错误,但我只看到黑屏而不是图像。
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyTask mt = new MyTask();
mt.execute();
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/askeroid/logos/1_mobile.png");
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
}
}
类 MyTask 扩展 AsyncTask {
@Override
protected Void doInBackground(Void... params) {
try{
URL url = new URL("http://ed.sadko.mobi/logo/logo_1mobile.png");
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/askeroid/logos/1_mobile.png");
output.flush();
output.close();
input.close();
} catch(Exception e){e.printStackTrace();}
return null;
}
}