Java Code Examples for android.app.Activity#managedQuery()

The following examples show how to use android.app.Activity#managedQuery() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: wakao-app   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 获取SD卡中最新图片路径
 * 
 * @return
 */
public static String getLatestImage(Activity context) {
	String latestImage = null;
	String[] items = { MediaStore.Images.Media._ID,
			MediaStore.Images.Media.DATA };
	Cursor cursor = context.managedQuery(
			MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null,
			null, MediaStore.Images.Media._ID + " desc");

	if (cursor != null && cursor.getCount() > 0) {
		cursor.moveToFirst();
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
				.moveToNext()) {
			latestImage = cursor.getString(1);
			break;
		}
	}

	return latestImage;
}
 
Example 2
Source Project: KJFrameForAndroid   File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 把uri转为File对象
 */
public static File uri2File(Activity aty, Uri uri) {
    if (SystemTool.getSDKVersion() < 11) {
        // 在API11以下可以使用:managedQuery
        String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor actualimagecursor = aty.managedQuery(uri, proj, null, null,
                null);
        int actual_image_column_index = actualimagecursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        String img_path = actualimagecursor
                .getString(actual_image_column_index);
        return new File(img_path);
    } else {
        // 在API11以上:要转为使用CursorLoader,并使用loadInBackground来返回
        String[] projection = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(aty, uri, projection, null,
                null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return new File(cursor.getString(column_index));
    }
}
 
Example 3
Source Project: monolog-android   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 通过uri获取文件的绝对路�?
 *
 * @param uri
 * @return
 */
@SuppressWarnings("deprecation")
public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = context.managedQuery(uri, proj, // Which columns to
            // return
            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;
}
 
Example 4
Source Project: monolog-android   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 获取图片缩略�? 只有Android2.1以上版本支持
 *
 * @param imgName
 * @param kind    MediaStore.Images.Thumbnails.MICRO_KIND
 * @return
 */
@SuppressWarnings("deprecation")
public static Bitmap loadImgThumbnail(Activity context, String imgName,
                                      int kind) {
    Bitmap bitmap = null;

    String[] proj = {MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME};

    Cursor cursor = context.managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
            MediaStore.Images.Media.DISPLAY_NAME + "='" + imgName + "'",
            null, null);

    if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
        ContentResolver crThumb = context.getContentResolver();
        Options options = new Options();
        options.inSampleSize = 1;
        bitmap = getThumbnail(crThumb, cursor.getInt(0),
                kind, options);
    }
    return bitmap;
}
 
Example 5
Source Project: monolog-android   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 获取SD卡中�?新图片路�?
 *
 * @return
 */
public static String getLatestImage(Activity context) {
    String latestImage = null;
    String[] items = {MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA};
    Cursor cursor = context.managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null,
            null, MediaStore.Images.Media._ID + " desc");

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                .moveToNext()) {
            latestImage = cursor.getString(1);
            break;
        }
    }

    return latestImage;
}
 
Example 6
Source Project: wakao-app   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 获取图片缩略图 只有Android2.1以上版本支持
 * 
 * @param imgName
 * @param kind
 *            MediaStore.Images.Thumbnails.MICRO_KIND
 * @return
 */
public static Bitmap loadImgThumbnail(Activity context, String imgName,
		int kind) {
	Bitmap bitmap = null;

	String[] proj = { MediaStore.Images.Media._ID,
			MediaStore.Images.Media.DISPLAY_NAME };

	Cursor cursor = context.managedQuery(
			MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
			MediaStore.Images.Media.DISPLAY_NAME + "='" + imgName + "'",
			null, null);

	if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
		ContentResolver crThumb = context.getContentResolver();
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 1;
		bitmap = MethodsCompat.getThumbnail(crThumb, cursor.getInt(0),
				kind, options);
	}
	return bitmap;
}
 
Example 7
/**
 * 把uri转为File对象
 *
 * @param activity Activity
 * @param uri      文件Uri
 * @return File对象
 */
public static File uri2File(Activity activity, Uri uri) {
    if (Build.VERSION.SDK_INT < 11) {
        // 在API11以下可以使用:managedQuery
        String[] proj = {MediaStore.Images.Media.DATA};
        @SuppressWarnings("deprecation")
        Cursor actualimagecursor = activity.managedQuery(uri, proj, null, null,
                null);
        int actual_image_column_index = actualimagecursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        String img_path = actualimagecursor
                .getString(actual_image_column_index);
        return new File(img_path);
    } else {
        // 在API11以上:要转为使用CursorLoader,并使用loadInBackground来返回
        String[] projection = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(activity, uri, projection, null,
                null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return new File(cursor.getString(column_index));
    }
}
 
Example 8
Source Project: VideoMeeting   File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 把媒体文件uri转为File对象
 */
public static File uri2File(Activity aty, Uri uri) {
    if (android.os.Build.VERSION.SDK_INT < 11) {
        // 在API11以下可以使用:managedQuery
        String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor actualimagecursor = aty.managedQuery(uri, proj, null, null,
                null);
        int actual_image_column_index = actualimagecursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        String img_path = actualimagecursor
                .getString(actual_image_column_index);
        return new File(img_path);
    } else {
        // 在API11以上:要转为使用CursorLoader,并使用loadInBackground来返回
        String[] projection = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(aty, uri, projection, null,
                null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return new File(cursor.getString(column_index));
    }
}
 
Example 9
Source Project: zone-sdk   File: PhotoUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 获取SD卡中最新图片路径
 *
 * @return
 */
public static String getLatestImage(Activity context) {
    String latestImage = null;
    String[] items = { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null,
            null, MediaStore.Images.Media._ID + " desc");

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                .moveToNext()) {
            latestImage = cursor.getString(1);
            break;
        }
    }
    return latestImage;
}
 
Example 10
Source Project: zone-sdk   File: PhotoUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 通过uri获取文件的绝对路径
 *
 * @param uri
 * @return
 */
public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, proj, // Which columns to
            // return
            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;
}
 
Example 11
Source Project: wakao-app   File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 通过uri获取文件的绝对路径
 * 
 * @param uri
 * @return
 */
public static String getAbsoluteImagePath(Activity context, Uri uri) {
	String imagePath = "";
	String[] proj = { MediaStore.Images.Media.DATA };
	Cursor cursor = context.managedQuery(uri, proj, // Which columns to
													// return
			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;
}
 
Example 12
Source Project: iBeebo   File: Utility.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getPicPathFromUri(Uri uri, Activity activity) {
    String value = uri.getPath();

    if (value.startsWith("/external")) {
        String[] proj = {
                MediaStore.Images.Media.DATA
        };
        Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else {
        return value;
    }
}
 
Example 13
Source Project: letv   File: SystemUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String getRealPathFromUri(Activity activity, Uri uri) {
    Cursor managedQuery = activity.managedQuery(uri, new String[]{"_data"}, null, null, null);
    if (managedQuery == null) {
        return null;
    }
    int columnIndexOrThrow = managedQuery.getColumnIndexOrThrow("_data");
    managedQuery.moveToFirst();
    return managedQuery.getString(columnIndexOrThrow);
}
 
Example 14
Source Project: Common   File: FileUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 把uri转为File对象
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static File uri2File(Activity aty, Uri uri) {
    if (SystemTool.getSDKVersion() < 11) {
        // 在API11以下可以使用:managedQuery
        String[] proj = {MediaStore.Images.Media.DATA};
        @SuppressWarnings("deprecation")
        Cursor actualimagecursor = aty.managedQuery(uri, proj, null, null,
                null);
        int actual_image_column_index = actualimagecursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        String img_path = actualimagecursor
                .getString(actual_image_column_index);
        return new File(img_path);
    } else {
        // 在API11以上:要转为使用CursorLoader,并使用loadInBackground来返回
        String[] projection = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(aty, uri, projection, null,
                null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return new File(cursor.getString(column_index));
    }
}
 
Example 15
/**
 * 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使
 */
public static String getPath(Activity context,final Uri uri){
    if (Build.VERSION.SDK_INT < 19) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor actualimagecursor = context.managedQuery(uri, proj, null, null, null);
        int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualimagecursor.moveToFirst();
        return actualimagecursor.getString(actual_image_column_index);
    } else {
        return getPathKitkat(context, uri);
    }
}
 
Example 16
public static String getRealPathFromURI(Uri contentUri, Activity activity) {
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
 
Example 17
Source Project: iBeebo   File: Utility.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getPicPathFromUri(Uri uri, Activity activity) {
    String value = uri.getPath();

    if (value.startsWith("/external")) {
        String[] proj = {
                MediaStore.Images.Media.DATA
        };
        Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else {
        return value;
    }
}
 
Example 18
Source Project: whiteboard   File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * ��ȡURI�а������ļ�·��
 * 
 * @param contentUri
 * @param activity
 * @return
 */
public String getRealPathFromURI(Uri contentUri, Activity activity)
{
	String[] proj = { MediaStore.Images.Media.DATA };
	Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
	int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	cursor.moveToFirst();

	return cursor.getString(column_index).replace(" ", "%20");
}
 
Example 19
public static String getPath(Activity activity, Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    activity.startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
 
Example 20
Source Project: iBeebo   File: Utility.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getPicPathFromUri(Uri uri, Activity activity) {
    String value = uri.getPath();

    if (value.startsWith("/external")) {
        String[] proj = {
                MediaStore.Images.Media.DATA
        };
        Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else {
        return value;
    }
}