点击进入相册 选择照片并 获取返回值 显示图片

在 最新开源作品 大开Note


地址:https://github.com/mhgd3250905/DaKaiNote


中有需要点击进入相册 然后对图片进行选择 然后显示在布局中 ,代码如下:

// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);

在Activity中获取返回值

if (resultCode == RESULT_OK){
    switch (requestCode){
        case PHOTO_REQUEST_GALLERY:
            if (data!=null){
                fl_note_iamge.setVisibility(View.VISIBLE);
                //设置图片存在
                isImageExist = true;
                Uri uriImageFromGallery = data.getData();
                LogUtils.Log(CameraImageUtils.getAbsoluteImagePath(ArcNewNoteActivity.this,
                        uriImageFromGallery));

                ImageCache imageCacheGallery = new ImageCache();
                imageCacheGallery.setNoteKey(noteKey);
                imageCacheGallery.setImagePath(CameraImageUtils.getAbsoluteImagePath(ArcNewNoteActivity.this,
                        uriImageFromGallery));
                imageCacheGallery.save();
                //获取当前fragment

                mImageNewNoteFragment.insertImage(noteKey);
            }
            break;
    }
}
这里通过一个方法 来通过图片Uri来获取图片的绝对路径

这个方法如下:


/**
     * 通过uri获取文件的绝对路径
     *
     * @param uri
     * @return
     */
    private String getAbsoluteImagePath(Uri uri) {
        String imagePath = "";
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = MainActivity.this.managedQuery(uri, proj, // Which
                null, // WHERE clause; which rows to return (all rows)
                null, // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)

        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                imagePath = cursor.getString(column_index);
            }
        }
        return imagePath;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值