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

/**
 * Converts the content:// scheme to the file path
 * @param contentResolver Provides access to the content model
 * @param contentUri The URI to be converted using content:// scheme
 * @return The converted file path//from  ww  w. ja  v  a2  s. c o m
 */
public static String getPathFromContentUri(ContentResolver contentResolver, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, projection, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get the file path from a Media storage URI.
 *//* w ww.ja  va  2  s  .c  om*/
public static String getPathFromURI(ContentResolver contentResolver, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }
    try {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (!cursor.moveToFirst()) {
            return null;
        } else {
            return cursor.getString(columnIndex);
        }
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, ContentResolver content_resolver) {
    Cursor cursor = null;
    try {/* w  w  w  .  j  a v  a2s .co  m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = content_resolver.query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, Context ctx) {
    Log.d("thong", "Uri: " + contentUri.toString());

    try {/* w ww.j a v  a2  s .c om*/
        String realpath = "";

        String[] proj = { MediaStore.Images.Media.DATA };

        //Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);

        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);

        Log.d("thong", "Column count: " + cursor.getColumnCount());

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        realpath = cursor.getString(column_index);

        cursor.close();

        return realpath;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

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

From source file:Main.java

public final static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {// www .j a  va 2  s .  c o  m
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

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

From source file:Main.java

public static File getRealFileFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {//from   www . j  av a2  s . com
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        return new File(path);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 */// w w w.  ja v a  2s.  com
public static String getRealPathFromURI(Activity activity, Uri contentUri) {

    String releaseNumber = Build.VERSION.RELEASE;

    if (releaseNumber != null) {
        /* ICS Version */
        if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            CursorLoader cursorLoader = new CursorLoader(activity, contentUri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
        /* GB Version */
        else if (releaseNumber.startsWith("2.3")) {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
    }

    //---------------------
    // Undefined Version
    //---------------------
    /* GB, ICS Common */
    String[] proj = { MediaStore.Images.Media.DATA };
    String strFileName = "";
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        // Use the Cursor manager in ICS         
        activity.startManagingCursor(cursor);

        cursor.moveToFirst();
        if (cursor.getCount() > 0)
            strFileName = cursor.getString(column_index);

        //cursor.close(); // If the cursor close use , This application is terminated .(In ICS Version)
        activity.stopManagingCursor(cursor);
    }
    return strFileName;
}

From source file:Main.java

/**
 * Get a file path from a Uri.//from   w  w  w  . j  a  v a2s .  c om
 * 
 * @param context
 * @param uri
 * @return
 * @throws URISyntaxException
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) throws URISyntaxException {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}