ANDROID 4.4以上:
@TargetApi(19)private void handleImageOnKitKat(Intent data){    String imagePath = null;    Uri uri = data.getData();    if(DocumentsContract.isDocumentUri(this,uri)){        String docID = DocumentsContract.getDocumentId(uri);        if("com.android.providers.media.documents".equals(uri.getAuthority()))        {            String id = docID.split(":")[1];            String selection = MediaStore.Images.Media._ID + "=" +id;            imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);            }        else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docID));            imagePath = getImagePath(contentUri,null);            }        }        else if("content".equalsIgnoreCase(uri.getScheme()))            {            imagePath = getImagePath(uri,null);             }        else if ("file".equalsIgnoreCase(uri.getScheme())){            imagePath = uri.getPath();         }        displayImage(imagePath);    }

4.4以下

private void handleImageBeforeKitKat(Intent data){    Uri uri = data.getData();    String imagePath = getImagePath(uri,null);    displayImage(imagePath);    }


getimagePath函数

private String getImagePath(Uri uri, String selection){    String path = null;    Cursor cursor = getContentResolver().query(uri,null,selection,null,null);    if(cursor != null){        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));    }    cursor.close();    return path;}

display函数

private void displayImage(String imagePath){    if(imagePath != null){        BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize = 2;        Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options);        showview.setImageBitmap(bitmap);    }    else{        Toast.makeText(this,"failed to get image",Toast.LENGTH_SHORT).show();    }    }}



更多相关文章

  1. C语言函数以及函数的使用
  2. H5 Web网页通过JS(JavaScript)脚本调用Android本地原生方法函数
  3. android 回调函数二:应用实例
  4. android NDK JNI设置自己的log输出函数
  5. Android build/envsetup.sh 脚本分析(lunch函数)
  6. Android Hook学习之ptrace函数的使用
  7. Android中回调函数的理解---本人Android纯新手
  8. 【Android NDK 开发】Ubuntu 函数库交叉编译 ( Android 动态库交
  9. Android学习札记12:对Parcelable中describeContents()函数的一种

随机推荐

  1. Android(安卓)四大组件之 Service(一)
  2. android地图定位
  3. Android(安卓)android:allowBackup waiti
  4. Android—— 4.2 Vold挂载管理_主体构建m
  5. [Android][SystemUI]navigationbar 3个虚
  6. Android预安装软件&adb命令&编译源码
  7. 81.s1-禁用checkBox点击事件
  8. android驱动学习-led次设备号(2)
  9. Android(安卓)分析:Process xxxxx (pid 30
  10. Android(安卓)准确过滤(禁止) Emoji表情