媒体库选择图片显示可提供上传(非sdcard)

(written at 2013-03-19 09:23:51)


最近要在项目中要实现这种功能,在网上也没有找到很好的例子,参考了各种例子终于做出来一个比较像样的,但是还有一个小问题,就是当选用非系统媒体库方式打开时候不能进行裁剪,之前有试过可以裁剪的,但是方法被我不知道弄到哪里去了,现在分享个给大家参考。

在没有sd卡的情况下怎么去调用媒体库的图片文件,或者是直接选用其他图片浏览器,文件管理器进行选图片。
1.先创建一个图片在项目的安装目录下,也就是/data/data/com.公司名.项目名称/目录下,我这里所选的是files目录。

String fileName = "temp.jpg";
File temp = this.getFilesDir(); // 即/data/data/com.公司名.项目名称/files
tempFile = new File(temp.getAbsolutePath(),fileName);
try {
output = openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE);
output.write(getBytes(tempFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tempFile = FileUtils.createFile(temp.getAbsolutePath(), fileName);



tempFile 是一个File类型的全局变量,因为这个文件是临时创建的,当不需要再上传图片的时候,可以把这个文件删除,直接调用tempFile.delete(),可以为手机内存存储节省空间。

2.调用媒体库的裁剪器

Intent intent = new Intent();
// 开启Pictures画面Type设定image
intent.setType("image/*");
intent.putExtra("crop", "true");
// 裁剪框比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", Dp2Px(this,88));
intent.putExtra("outputY", Dp2Px(this,88));
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
//使用媒体库
intent.setAction(Intent.ACTION_GET_CONTENT); 
//取得相片后返回本画面
startActivityForResult(intent, CHOOSE_PICTURE_REQUEST_CODE);



Dp2Px函数的作用是,将dp转换为像素,我这里的要求是88X88dp,outputX,outputY这里填的都是像素值,如果你不需要做这种类型转换就不需做无用功。CHOOSE_PICTURE_REQUEST_CODE是一个自定义的int类型常量,大于等于0即可。Intent.ACTION_GET_CONTENT的意思是使用媒体库,有些软件也注册了具有媒体功能,所以设置了此选项的时候可能会出现其他软件,比如一些图片浏览器,文件浏览器。要同时处理对其他软件对图片进行操作,我在下面是这样处理的。

3.重写onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(resultCode == CHOOSE_PICTURE_REQUEST_CODE){
//定位uri为非系统媒体库浏览器所打开的文件uri地址
Uri uri = data.getData();  
//如果uri不为空,则取到了图片文件地址
if(uri!=null && !uri.equals("")){
//通过文件流的方式获取图片
ContentResolver cr = this.getContentResolver();  
try {  
          Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));  
   //获取到的是图片
          if(bitmap !=null){
   //当获取图片可能比需求大或者小,进行一次固定大小的裁剪,转化为88X88dp(根据实际需要)
  int dp =Dp2Px(this,88);
   Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, dp, dp, true);
    //将Bitmap设定到ImageView
    mPhotoImageView.setImageBitmap(mBitmap);

    FileOutputStream  fOut = new FileOutputStream(tempFile);
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
   }else{
       //获取到的不是图片的处理
   }
} catch (FileNotFoundException e) {  
        Log.e("Exception", e.getMessage(),e);  
}  
//这里是针对选择系统媒体库的操作
}else if(tempFile!=null && !tempFile.equals("")){
byte[] myIcon = getBytes(tempFile);
//设置contact新的头像
if(myIcon !=null && !myIcon.equals("")){
Bitmap map = BitmapFactory.decodeByteArray(myIcon, 0, myIcon.length);
mPhotoImageView.setImageBitmap(map);
}
}            
}
}
super.onActivityResult(requestCode, resultCode, data);
}


getBytes是一个自定义方法,将file转化为byte[]。
4.在最后不使用此功能,可以把tempFile删除:tempFile.delete()。

如果大家还有什么更好的实现方法欢迎跟我一起讨论。Email:arjinmc@hotmail.com


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值