java传输本地图片到Android

2017-09-20  本文已影响0人  ffday

实现将本地存储的图片发送到后台

使用springMVC框架,获取本地图片,转换成流输出

@RequestMapping(value="showImageDiaLog.do",params={"action=showImageDiaLog"})
    public void showImageByType(String filename,HttpServletRequest request,HttpServletResponse response) throws IOException{  
        InputStream inputStream = null;  
        OutputStream writer = null; 
        filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");
        try {  
                inputStream = new FileInputStream(new File(Constants.getApplicationValue("C\:\\\myeclipseWork\\image\\")+filename));
                writer = response.getOutputStream();  
                byte[] buf = new byte[1024];  
                int len = 0;  
                while ((len = inputStream.read(buf)) != -1) {  
                   writer.write(buf, 0, len); //写  
                 }  
            } catch (Exception e) {  
                logger.error(e.toString());
            } finally{  
                 if(inputStream != null){  
                     inputStream.close();  
                 }  
                 if(writer != null){  
                     writer.close();  
                 }  
         }  
    }  

Android代码

请求地址,出入请求的文件名

private String url = "http://192.168.1.132:8080/eSAM/showImageDiaLog.do?action=showImageDiaLog&filename=1.jpg";

使用glide框架获取图片展示

  Glide.with(MainActivity.this)
                .load(url)
                .into(imageView);

保存在本地,工具类

package com.example.image;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by user on 20/9/2017.
 */

public class SDFileHelper {
    private Context context;

    public SDFileHelper() {
    }

    public SDFileHelper(Context context) {
        super();
        this.context = context;
    }

    //Glide保存图片
    public void savePicture(final String fileName, String url){
        Glide.with(context).load(url).asBitmap().toBytes().into(new SimpleTarget<byte[]>() {
            @Override
            public void onResourceReady(byte[] bytes, GlideAnimation<? super byte[]> glideAnimation) {
                try {
                    savaFileToSD(fileName,bytes);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, byte[] bytes) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath = Environment.getExternalStorageDirectory().getCanonicalPath()+"/budejie";
            File dir1 = new File(filePath);
            if (!dir1.exists()){
                dir1.mkdirs();
            }
            filename = filePath+ "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(bytes);
            //将bytes写入到输出流中
            output.close();
            //关闭输出流
            Toast.makeText(context, "图片已成功保存到"+filePath, Toast.LENGTH_SHORT).show();
            Log.e("filePath:",filePath);
        } else {
            Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
        }
    }
}

保存在sd卡

 //保存图片在本地
        SDFileHelper helper = new SDFileHelper(MainActivity.this);
        helper.savePicture("b.jpg",url);
上一篇下一篇

猜你喜欢

热点阅读