Android 异步下载

复制代码
package com.example.demo1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.tiffdecoder.TiffDecoder;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;

public class DownloadImagesTask extends AsyncTask<Object, Void, Bitmap> {
    ImageView imageView = null;
    Activity activity = null;
    
    @Override
    protected Bitmap doInBackground(Object... parameters) {
        this.imageView = (ImageView)parameters[0];
        this.activity = (Activity)parameters[1];
        try {
            return download_Image((String)imageView.getTag());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(Bitmap result) {
        if(result != null)
            imageView.setImageBitmap(result);
    }
    
    private Bitmap download_Image(String url) throws IOException {
        File file = downloadFile(url);
        TiffDecoder.nativeTiffOpen(file.getPath());
        int[] pixels = TiffDecoder.nativeTiffGetBytes();
        Bitmap mBitmap = Bitmap.createBitmap(pixels, TiffDecoder.nativeTiffGetWidth(), TiffDecoder.nativeTiffGetHeight(),Bitmap.Config.ARGB_8888);
        TiffDecoder.nativeTiffClose();
        return mBitmap;
    }
    
    private File downloadFile(String strUrl) throws IOException
    {
        URL url = new URL(strUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        
        File file = File.createTempFile("myfile", ".tif", this.activity.getCacheDir());
        if(file.exists())
            file.delete();
        file.createNewFile();

        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
        return file;
    }
}
复制代码
复制代码
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.setThreadPolicy(new 
                StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
                        StrictMode.setVmPolicy(
                new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        
                        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ImageView image = (ImageView)this.findViewById(R.id.image);
        image.setTag("http://xibei1-image.xxx.com/00006009b4673bbc-ff2a-49bd-91fe-764c01b2acce01.tif");
        new DownloadImagesTask().execute(image,this);
    }
}
复制代码

 

posted on   空明流光  阅读(259)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示