Asynctask下载Bitmap图像时的存储位置
创始人
2024-09-21 10:01:04
0

在 Asynctask 中下载 Bitmap 图像并将其存储在磁盘上,可以使用以下代码示例:

class DownloadImageTask extends AsyncTask {
    private String filePath;

    public DownloadImageTask(String filePath) {
        this.filePath = filePath;
    }

    protected Bitmap doInBackground(String... urls) {
        String imageUrl = urls[0];
        Bitmap bitmap = null;
        try {
            InputStream in = new java.net.URL(imageUrl).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            try {
                File file = new File(filePath);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream out = new FileOutputStream(file);
                result.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

在调用 AsyncTask 时,传递一个文件路径作为参数,例如:

new DownloadImageTask("/sdcard/myimage.png").execute("http://example.com/image.png");

这将从 http://example.com/image.png 下载图像并将其保存在 /sdcard/myimage.png 中。请确保在 AndroidManifest.xml 中声明相应的权限。

相关内容

热门资讯

aapoker外挂!wpk有机... aapoker外挂!wpk有机器人吗,微扑克ai机器人打德州,可靠教程(有挂了解)1)微扑克ai机器...
德扑起手牌胜率图!微扑克系统发... 德扑起手牌胜率图!微扑克系统发牌规律,wpk真的太假了,实用技巧(有挂规律)1、构建自己的微扑克辅助...
wepoke辅助挂!wpk真的... wepoke辅助挂!wpk真的有挂么,wepoke软件透明挂可以弄,教你攻略(有挂技巧)是一款可以让...
wepokeai代打的胜率!w... 1、wepokeai代打的胜率!wpk透视辅助器,wepoke有挂网上靠谱,细节揭秘(有挂教程);代...
wepoke辅助有挂!wpk微... wepoke辅助有挂!wpk微扑克俱乐部,微扑克发牌算法,教你攻略(有挂攻略)1)wepoke辅助有...
wepower有外挂!红龙扑克... wepower有外挂!红龙扑克辅助工具,wepokeai代打,2025新版教程(有挂透明)1、wep...
德扑ai助手!wepoke支持... 德扑ai助手!wepoke支持安卓吗,wepoke用模拟器,实用技巧(有挂攻略)德扑ai助手辅助器中...
微扑克有辅助挂!wpk德州职业... 微扑克有辅助挂!wpk德州职业代打,wpk俱乐部盈利模式,科技教程(有挂技巧);人气非常高,ai更新...
aapoker辅助!aapok... aapoker辅助!aapoker辅助工具存在吗,红龙扑克是真正规的,我来教教你(有挂教学)1.aa...
德州微扑克辅助!德州poker... 德州微扑克辅助!德州poker有外挂吗,德州aapoker俱乐部外挂,可靠教程(有挂规律)是一款可以...