1. AndroidManifest.XML中添加权限

 

2. 使用Intent

Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,

然后设置类型为image/*”,就可获得android手机内的所有image

Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_GET_CONTENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("image/*");

onCreate中开启onActivityResult

startActivityForResult(intent1,111);

复写onActivityResult读取选择图片的uri

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    switch (requestCode){
        case 111:
            if(resultCode ==RESULT_CANCELED) {
                Toast.makeText(getApplication(), "点击取消从相册选择", Toast.LENGTH_LONG).show();
                return;
            }
            try{
                Uri uri = data.getData();
                Log.e("TAG",uri.toString());

                String filePath = getRealPathFromURI(uri);
                bitmap1 = getresizePhoto(filePath);
                imageView1.setImageBitmap(bitmap1);
                if(bitmap1!=null){
                    Log.e("aa","bitmap1不为空!!!!!!!!!!");
                }else{
                    Log.e("aa","bitmap1为空!!!!!!!!!!");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            break;
    }
}

某个uri格式:uri: content://media/external/images/media/614016

某个图片路径 /storage/emulated/0/DCIM/P70713-115542.jpg

URI获取String类型的文件路径

public String getRealPathFromURI(Uri contentUri){
    Cursor cursor = null;
    try{

        String [] proj = {MediaStore.Images.Media.DATA};

//由context.getContentResolver()获取contentProvider再获取cursor(游

//标)用游标获取文件路径返回
        cursor = context.getContentResolver().query(contentUri,proj,null,null,null);
        cursor.moveToFirst();
        int column_indenx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        return cursor.getString(column_indenx);
    } finally {

        if (cursor != null) {

            cursor.close();
        }
    }
}

 

3. 根据文件路径调整图片大小防止OOM并且返回bitmap

private  Bitmap getresizePhoto(String ImagePath){
    if (ImagePath!=null){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(ImagePath,options);
        double ratio = Math.max(options.outWidth*1.0d/1024f,options.outHeight*1.0d/1024);
        options.inSampleSize = (int) Math.ceil(ratio);
        options.inJustDecodeBounds= false;
        Bitmap bitmap=BitmapFactory.decodeFile(ImagePath,options);
        return bitmap;
    }
    return null;
}

更多相关文章

  1. Android(安卓)Kikat下由图片Uri获取Path的解决方案
  2. Android(安卓)Studio 关联源码配置方法
  3. Android:默认手机存储路径为TF卡
  4. bitmap与canvas android
  5. flutter高德地图使用
  6. Android(安卓)Binder分析三:Natvie Service的获取和调用
  7. android xml文件中进行上传图片以及获取图片
  8. Android(安卓)获取远程服务器时间
  9. Android(安卓)O Launcher3-Workspace加载

随机推荐

  1. android 插件实现
  2. Android(安卓)xxx is not translated in
  3. Android涓璙iew缁樺埗娴佺▼鍒嗘瀽
  4. 用tcpdump抓取Android的网络数据包
  5. Android(安卓)SVG动画PathView源码解析与
  6. android濡備綍鍒╃敤aspectj杩涜AOP 缂
  7. android 的gradle
  8. Android初始化过程
  9. Android(安卓)Studio 添加arr为依赖
  10. 把第三方jar库加入android framework ser