Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

In this page you can find the example usage for android.database Cursor getColumnIndexOrThrow.

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:Main.java

public static Uri getBitmapFromImageId(Activity activity, int imageId) {
    String[] tinyImgPprojection = { MediaStore.Images.Thumbnails._ID };
    Cursor tinyCursor = Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
            Thumbnails.MINI_KIND, tinyImgPprojection);
    if (tinyCursor.getCount() != 0) {
        tinyCursor.moveToFirst();/*from  w  ww . j a v  a  2 s  . c  o m*/
        int tinyImgId = tinyCursor.getInt(tinyCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        tinyCursor.close();
        return Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                String.valueOf(tinyImgId));
    } else {
        tinyCursor.close();
        return null;
    }
}

From source file:Main.java

/**
 * Return file size from Uri/* www . j  ava 2 s  .com*/
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:com.radadev.xkcd.Comics.java

public static void downloadComic(Integer comicNumber, Context context)
        throws FileNotFoundException, IOException {
    ComicDbAdapter dbAdapter = new ComicDbAdapter(context);
    dbAdapter.open();/*from ww w  .  j  a  va  2s  .  co  m*/
    try {
        dbAdapter.updateComic(comicNumber);
        File file = new File(getSdDir(context), comicNumber.toString() + ".png");
        if (file.length() <= 0) {
            Cursor cursor = dbAdapter.fetchComic(comicNumber);
            String url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));

            if (url == null || url.length() == 0) {
                dbAdapter.updateComic(comicNumber);
                cursor.close();
                cursor = dbAdapter.fetchComic(comicNumber);
                url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));
            }
            cursor.close();

            Bitmap bitmap = Comics.downloadBitmap(url);
            FileOutputStream fileStream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.PNG, 100, fileStream);
            bitmap.recycle();
            fileStream.close();
        }
    } finally {
        dbAdapter.close();
    }
}

From source file:Main.java

/**
 * Return file display name from Uri/*from  ww  w . java  2 s  .  co  m*/
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *//*from  w  ww . ja  v a2 s  . com*/
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}

From source file:org.hfoss.posit.android.web.PositHttpUtils.java

public static List<NameValuePair> convertFindsForPost(String[] projection, Cursor c) {
    List<NameValuePair> finds = new ArrayList<NameValuePair>();
    c.moveToFirst();//from w ww  .j  ava  2  s  .com
    try {
        for (String item : projection) {
            finds.add(new BasicNameValuePair(item, c.getString(c.getColumnIndexOrThrow(item))));
        }
    } catch (CursorIndexOutOfBoundsException e) {
        if (Utils.debug)
            Log.e(TAG, e.getMessage());
    }
    return finds;
}

From source file:Main.java

public static String uriTitle(Context context, String contentUri) {
    String result = null;//from  w ww  .j  a  va  2  s  .c  o  m
    String[] p = { MediaStore.MediaColumns.TITLE };
    Uri uri = Uri.parse(contentUri);
    if (!uri.getScheme().equals("content")) {
        return (fileUriTitle(context, contentUri));
    }
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
        if (cursor.moveToFirst()) {
            result = cursor.getString(iColumn);
        }
    }
    return (result);
}

From source file:Main.java

public static long uriFileSize(Context context, String contentUri) {
    long result = 0;
    Uri uri = Uri.parse(contentUri);//from  www.ja  v a2  s  .  co  m
    if (!uri.getScheme().equals("content")) {
        return (fileUriFileSize(context, contentUri));
    }
    String[] p = { MediaStore.MediaColumns.SIZE };
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
        if (cursor.moveToFirst()) {
            result = cursor.getLong(iColumn);
        }
    }
    return (result);
}

From source file:Main.java

public static File convertImageUriToFile(Uri imageUri, Activity activity) {
    Cursor cursor = null;
    try {/* ww w  . ja v a  2s.co  m*/
        String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.ORIENTATION };
        cursor = activity.managedQuery(imageUri, proj, null, null, null);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            String orientation = cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static String getPathFromUri(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }/*  w w w .  j a v  a  2 s .co  m*/
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            return null;
        }
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}