zoukankan      html  css  js  c++  java
  • Android 照片上传

    解释全在代码中:

    // 拍照上传
    private OnClickListener mUploadClickListener = new OnClickListener() {
    
    public void onClick(View v) {
    
     
    
    // 调用相机
    
     
    
    Intent mIntent = new Intent("android.media.action.IMAGE_CAPTURE");
    
     
    
     
    
    // 图片存储路径,可自定义
    
     
    
    File tmpFile = new File(Environment.getExternalStorageDirectory(),
    
     
    
    System.currentTimeMillis() + ".jpg");
    
     
    
     
    
    // 获取这个图片的URI
    
     
    
    originalUri = Uri.fromFile(tmpFile);//这是个实例变量,方便下面获取图片的时候用
    
     
    
    mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
    
     
    
     
    
    startActivityForResult(mIntent, ACTIVITY_IMAGE_CAPTURE);
    
     
    
    }
    
     
    
    };
    
     
    
     
    
    // 打开相册
    
     
    
    private OnClickListener mPicListClickListener = new OnClickListener() {
    
     
    
     
    
    public void onClick(View v) {
    
     
    
    // 调用相册
    
     
    
    Intent mIntent= new Intent(Intent.ACTION_GET_CONTENT);
    
     
    
    mIntent.addCategory(Intent.CATEGORY_OPENABLE);
    
     
    
    mIntent.setType(MIME_TYPE_IMAGE_JPEG);
    
     
    
     
    
    startActivityForResult(mIntent, ACTIVITY_GET_IMAGE);
    
     
    
    }
    
     
    
    };
    
     
    
     
    
    监听事件写好了,怎么调用不用我说了吧。这是个startActivityForResult事件,对应的我们肯定得有个onActivityResult,贴之
    
     
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
     
    
     
    
    if (resultCode != RESULT_OK) {
    
     
    
    return;
    
     
    
    }
    
     
    
     
    
    Bitmap bm = null;
    
     
    
     
    
    ContentResolver resolver = getContentResolver();
    
     
    
    String filePath = "/sdcard/bengxin/bx_upload_tmp.jpg";//这个是暂存图片的路径
    
     
    
    FileOutputStream output = null;
    
     
    
    try {
    
     
    
    // 创建暂存图片
    
     
    
    if (Utils.CreateFile(filePath)) {
    
     
    
    output = new FileOutputStream(filePath);
    
     
    
    } else {
    
     
    
    throw new Exception("内部错误");
    
     
    
    }
    
     
    
    if (requestCode == ACTIVITY_GET_IMAGE) {
    
     
    
    // 获得图片的uri
    
     
    
    originalUri = data.getData();
    
     
    
    PS:拍照的那个URI我们在上面已经获取了
    
     
    
    }
    
     
    
     
    
    /**** 获取图片开始 ****/
    
     
    
    //mContent是上传的图片byte[]数组,得到这个后随便怎么处理,当然你也可以直接用fileInput流
    
     
    
    fileInput = (FileInputStream) resolver.openInputStream(Uri
    
     
    
    .parse(originalUri.toString()));
    
     
    
     
    
    // 将图片内容解析成字节数组
    
     
    
    mContent = getBytesFromInputStream(fileInput, 3500000);
    
     
    
     
    
    fileInput.close();
    
     
    
     
    
    // 将字节数组转换为ImageView可调用的Bitmap对象
    
     
    
    bm = getPicFromBytes(mContent, null);
    
     
    
     
    
    /********* 获取图片完了 ************/
    
     
    
     
    
    // 将图片缩小到指定比例并且保存到缓存文件
    
     
    
    float scale = ((float) 210) / ((float) bm.getWidth());
    
     
    
    bm = Utils.smallBmp(bm, scale);//这个缩小功能是自己写的方法
    
     
    
    //将Bitmap读到文件中去,注意这个是压缩,那个100是压缩比,0-100,越大质量越好
    
     
    
    bm.compress(CompressFormat.JPEG, 100, output);
    
     
    
    output.flush();
    
     
    
    output.close();
    
     
    
     
    
    /*********为了更快速的将图片上传,将缩小后的图片保存到暂存文件***************/
    
     
    
    fileInput = new FileInputStream(filePath);
    
     
    
     
    
    // 将图片内容解析成字节数组
    
     
    
    mContent = getBytesFromInputStream(fileInput, 3500000);
    
     
    
    fileInput.close();
    
     
    
    /*********************/
    
     
    
     
    
    // 预览一下你的图片吧
    
     
    
    bm = bm.createScaledBitmap(bm, mButtomUpload.getWidth() - 10,
    
     
    
    mButtomUpload.getHeight() - 10, true);
    
     
    
    mButtomUpload.setImageBitmap(bm);
    
     
    
    mButtomUpload.setPadding(2, 2, 2, 2);
    
     
    
     
    
    } catch (Exception e) {
    
     
    
    Utils.exceptionShow(CheckIn.this, e.getMessage());
    
     
    
    }
    
     
    
    }
    
     
    
     
    
     
    
    附赠两个方法,一个将字节转换成bitmap,一个获取byte[]数组
    
     
    
    private static Bitmap getPicFromBytes(byte[] bytes,
    
    
    
    BitmapFactory.Options opts) {
    
     
    
     
    
    if (bytes != null)
    
     
    
    if (opts != null)
    
     
    
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
    
     
    
    opts);
    
     
    
    else
    
     
    
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    
     
    
    return null;
    
     
    
    }
    
     
    
     
    
    private static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
    
     
    
    throws IOException {
    
     
    
    int total = 0;
    
     
    
    byte[] bytes = new byte[4096];
    
     
    
    ByteBuffer bb = ByteBuffer.allocate(bufsiz);
    
     
    
     
    
    while (true) {
    
     
    
    int read = is.read(bytes);
    
     
    
    if (read == -1)
    
     
    
    break;
    
     
    
    bb.put(bytes, 0, read);
    
     
    
    total += read;
    
     
    
    }
    
     
    
     
    
    byte[] content = new byte[total];
    
     
    
    bb.flip();
    
     
    
    bb.get(content, 0, total);
    
     
    
     
    
    return content;
    
     
    
    } 

     转自https://blog.csdn.net/tao0001/article/details/7993007

  • 相关阅读:
    Android系统启动:1-综述
    在高通Fastmmi模式中增强交互方式
    Ubuntu 18.04安装xdrp以使用远程桌面
    如何在Android 确定 lunch对应的内核配置
    Android ADB命令集锦
    Android日志系统(logging system)
    汉诺塔游戏
    设置静态ip
    navicat的下载、激活
    上传本地文件到github(码云)上(小乌龟方式,sourcetree方式)
  • 原文地址:https://www.cnblogs.com/sengzhao666/p/11011100.html
Copyright © 2011-2022 走看看