根据网络路径存储图片并在图库显示

来源:互联网 发布:知否剧组照片 编辑:程序博客网 时间:2024/05/16 06:46
package com.example.administrator.mytest;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Date;public class MainActivity extends AppCompatActivity {    Bitmap bitmap;    ImageView imageView;    Handler handler;    Button button;    public static void saveImageToGallery(Context context, Bitmap bmp) {        // 首先保存图片        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");        if (!appDir.exists()) {            appDir.mkdir();        }        String fileName = System.currentTimeMillis() + ".jpg";        File file = new File(appDir, fileName);        try {            FileOutputStream fos = new FileOutputStream(file);            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();            Log.e("存储完成", "=====");        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        // 其次把文件插入到系统图库        try {            MediaStore.Images.Media.insertImage(context.getContentResolver(),                    file.getAbsolutePath(), fileName, null);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        // 最后通知图库更新        Log.e("文件存储的路径", file.getAbsolutePath());        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        imageView = (ImageView) findViewById(R.id.image);        handler = new Handler() {            @Override            public void handleMessage(Message msg) {                ;                imageView.setImageBitmap(bitmap);                Log.e("图片加载完成", "加载挖槽");                saveImageToGallery(MainActivity.this, bitmap);                super.handleMessage(msg);            }        };        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new Thread() {                    @Override                    public void run() {                        try {                            ///  bitmap= BitmapFactory.decodeStream(getImageStream("http://120.26.113.179:8089/UploadFiles/201704/05/1d15996e-4dba-4f84-9829-ac5c9771c4cc.png"));                            GetImageInputStream("http://120.26.113.179:8089/UploadFiles/201704/05/1d15996e-4dba-4f84-9829-ac5c9771c4cc.png");                            handler.sendEmptyMessage(1);                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                }.start();            }        });    }    /**     * 获取网络图片     *     * @param imageurl 图片网络地址     * @return Bitmap 返回位图     */    public Bitmap GetImageInputStream(String imageurl) {        URL url;        HttpURLConnection connection = null;        bitmap = null;        try {            url = new URL(imageurl);            connection = (HttpURLConnection) url.openConnection();            connection.setConnectTimeout(6000); //超时设置            connection.setDoInput(true);            connection.setUseCaches(false); //设置不使用缓存            InputStream inputStream = connection.getInputStream();            bitmap = BitmapFactory.decodeStream(inputStream);            inputStream.close();        } catch (Exception e) {            e.printStackTrace();        }        return bitmap;    }}

0 0
原创粉丝点击