资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android中怎么将图片路径与Uri相互转换

android中怎么将图片路径与Uri相互转换?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

余姚网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

一个android文件的Uri地址一般如下:

content://media/external/images/media/62026

这是一张图片的Uri,那么我们如何根据这个Uri获得其在文件系统中的路径呢?

其实很简单,直接上代码:

public static String getRealFilePath( final Context context, final Uri uri ) {
  if ( null == uri ) return null;
  final String scheme = uri.getScheme();
  String data = null;
  if ( scheme == null )
    data = uri.getPath();
  else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
    data = uri.getPath();
  } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
    Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );
    if ( null != cursor ) {
      if ( cursor.moveToFirst() ) {
        int index = cursor.getColumnIndex( ImageColumns.DATA );
        if ( index > -1 ) {
          data = cursor.getString( index );
        }
      }
      cursor.close();
    }
  }
  return data
  }

如我们有一个图片的路径地址又该如何获得其Uri呢?

  String type = Utils.ensureNotNull(intent.getType());
        Log.d(TAG, "uri is " + uri);
        if (uri.getScheme().equals("file") && (type.contains("image/"))) {
          String path = uri.getEncodedPath();
          Log.d(TAG, "path2 is " + path);
          if (path != null) {
            path = Uri.decode(path);
            Log.d(TAG, "path3 is " + path);
            ContentResolver cr = this.getContentResolver();
            StringBuffer buff = new StringBuffer();
            buff.append("(")
                .append(Images.ImageColumns.DATA)
                .append("=")
                .append("'" + path + "'")
                .append(")");
            Cursor cur = cr.query(
                Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { Images.ImageColumns._ID },
                buff.toString(), null, null);
            int index = 0;
            for (cur.moveToFirst(); !cur.isAfterLast(); cur
                .moveToNext()) {
              index = cur.getColumnIndex(Images.ImageColumns._ID);
              // set _id value
              index = cur.getInt(index);
            }
            if (index == 0) {
              //do nothing
            } else {
              Uri uri_temp = Uri
                  .parse("content://media/external/images/media/"
                      + index);
              Log.d(TAG, "uri_temp is " + uri_temp);
              if (uri_temp != null) {
                uri = uri_temp;
              }
            }
          }
        }

看完上述内容,你们掌握android中怎么将图片路径与Uri相互转换的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


新闻标题:android中怎么将图片路径与Uri相互转换
网站网址:http://cdkjz.cn/article/gjoiop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220