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

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

在做从系统选择图片并获取到它们的路径时发现有些图片的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;
	}


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