Android 学习笔记十三 通过绝对路径得到uri

	private static Uri queryMedia(String mediaPath, String path, Context context) {
        String StringUri = null;
        ContentResolver mContentResolver = context.getContentResolver();
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        StringUri = "content://media/external/" + path + "/media";

        
        
        Log.d(TAG,"mediaPath:"+mediaPath);
        Log.d(TAG,"path:"+path);
        Log.d(TAG,"path:"+StringUri);
        
        String searchString = mediaPath;
        Cursor c = null;
        try {
            searchString = Uri.decode(searchString).trim().replace("'", "''");

            Uri uri = Uri.parse(StringUri);
            c = mContentResolver.query(uri, new String[] {
                    "_id"
            }, "_data='" + searchString + "'", null, null);
            if (c != null && c.moveToNext()) {
                int id = c.getInt(0);
                return Uri.withAppendedPath(uri, String.valueOf(id));
            }
        } finally {
        	if (c != null) {
        		c.close();
        	}
        }
        return null;
    }

      通过上面的方法可以实现对于文件uri 的查找


    下面是一个具体使用的例子

   其中path 为文件的绝对路径

if (mimetype.startsWith("image/")) {
				path = queryMedia(
						path.toString().substring(7, path.toString().length()),
						"images", context);
				
				Log.d(TAG, "6666666 uri = " + path);
				Intent activityIntent = new Intent(Intent.ACTION_VIEW);
				mimetype = mimetype.toLowerCase();
				activityIntent.setDataAndType(path, mimetype);				
				activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				try {
					if (V)
						Log.d(TAG, "ACTION_VIEW intent sent out: " + path
								+ " / " + mimetype);
					context.startActivity(activityIntent);
				} catch (ActivityNotFoundException ex) {
					if (V)
						Log.d(TAG,
								"no activity for handling ACTION_VIEW intent:  "
										+ mimetype, ex);
				}
			}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值