Android之获取本地图片并压缩方法

这两天在做项目时,做到上传图片功能一块时,碰到两个问题,一个是如何获取所选图片的路径,一个是如何压缩图片,在查了一些资料和看了别人写的后总算折腾出来了,在此记录一下。

1 Intent intent = new Intent(Intent.ACTION_PICK, null);//从列表中选择某项并返回所有数据
2 intent.setDataAndType(
3                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//得到系统所有的图片
4                    "image/*");//图片的类型,image/*为所有类型图片
5 startActivityForResult(intent, PHOTO_GALLERY);

然后我们重写onActivityResult方法。

在Android1.5后系统会调用MediaScanner服务进行后台扫描,索引歌曲,图片,视频等信息,并将数据保存在android.provider.MediaStore.Images.Thumbnails 和android.provider.MediaStore.Video.Thumbnails这两个数据库中。

所以我们需要使用Activity.managedQuery(uri, projection, selection, selectionArgs, sortOrder)方法从数据中获取相应数据。

uri:  需要返回的资源索引

projection: 用于标识有哪些数据需要包含在返回数据中。

selection: 作为查询符合条件的过滤参数,类似于SQL语句中Where之后的条件判断。

selectionArgs: 同上。

sortOrder: 对返回信息进行排序。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
        //请求为获取本地图品时
        case PHOTO_GALLERY:
        {
                //图片信息需包含在返回数据中
                String[] proj ={MediaStore.Images.Media.DATA};
                //获取包含所需数据的Cursor对象                 
                @SuppressWarnings("deprecation")
                Cursor cursor = managedQuery(data.getData(), proj, null, null, null);
                //获取索引
                int photocolumn =  cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                //将光标一直开头
                cursor.moveToFirst();
                //根据索引值获取图片路径
                String path = cursor.getString(photocolumn);


            break;
        }
       
        default:
            break;
        }

以上,我们便可取得本地图片路径了,接下来我们队图片进行压缩处理。

//先将所选图片转化为流的形式,path所得到的图片路径
      FileInputStream is = new  FileInputStream(path);
      //定义一个file,为压缩后的图片
      File f = new File("图片保存路径","图片名称");
      int size = " ";
      Options options = new Options();
      options.inSampleSize = size;
      //将图片缩小为原来的  1/size ,不然图片很大时会报内存溢出错误
      Bitmap image = BitmapFactory.decodeStream(inputStream,null,options);

    is.close();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();               
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//这里100表示不压缩,将不压缩的数据存放到baos中
    int per = 100;               
    while (baos.toByteArray().length / 1024 > 500) { // 循环判断如果压缩后图片是否大于500kb,大于继续压缩
    baos.reset();// 重置baos即清空baos
    image.compress(Bitmap.CompressFormat.JPEG, per, baos);// 将图片压缩为原来的(100-per)%,把压缩后的数据存放到baos中
    per -= 10;// 每次都减少10
                   
    }
      //回收图片,清理内存
    if(image != null && !image.isRecycled()){
        image.recycle();
        image = null;
        System.gc();
        }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
    btout.close();
    FileOutputStream os;
    os = new FileOutputStream(f);
    //自定义工具类,将输入流复制到输出流中
    StreamTransferUtils.CopyStream(btinput, os);
    btinput.close();
    os.close();

完成以后,我们可以在指定的图片保存路径下看到压缩的图片。

猜你喜欢

转载自www.linuxidc.com/Linux/2016-05/131397.htm
今日推荐