码迷,mamicode.com
首页 > 移动开发 > 详细

从android相册获取所有图片的路径

时间:2015-08-14 13:47:47      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

在做从系统选择图片并获取到它们的路径时发现有些图片的uri.getScheme是“file”,有些图片的uri.getScheme是“content” 所有导致用uri.getPath并不能获取所有图片的路径,用如下代码解决:

public static String getPath(Activity activity, Uri uri) {
		L.i("hui", "处理前的路径:" + uri);
		if (null == uri) {
			return null;
		}
		String path = null;
		String scheme = uri.getScheme();
		if (scheme == null) {
			path = uri.getPath();
		} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
			path = uri.getPath();
		} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
			String[] projection = { MediaStore.Images.Media.DATA };
			Cursor cursor = activity.managedQuery(uri, projection, null, null,
					null);
			if (null != cursor) {
				int column_index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToFirst();
				path = cursor.getString(column_index);
				if (VERSION.SDK_INT < 14) { // android4.0及其以上的版本会自动关闭,不加会导致Attempted
											// to access a cursor after it has
											// been closed异常
					cursor.close();
				}
			}
		}
		return path;
	}


版权声明:本文为博主原创文章,未经博主允许不得转载。

从android相册获取所有图片的路径

标签:

原文地址:http://blog.csdn.net/runninghui/article/details/47659819

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!