Java Code Examples for android.content.CursorLoader#loadInBackground()

The following examples show how to use android.content.CursorLoader#loadInBackground() . 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 File: FileHelper.java    From showCaseCordova with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    try {
        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
    } catch (Exception e) {
        result = null;
    }
    return result;
}
 
Example 2
Source File: HashActivity.java    From CameraV with GNU General Public License v3.0 6 votes vote down vote up
public String getRealPathFromURI(Uri contentUri) {
 String[] proj = { MediaStore.Images.Media.DATA };
 
 //This method was deprecated in API level 11
 //Cursor cursor = managedQuery(contentUri, proj, null, null, null);
 
 CursorLoader cursorLoader = new CursorLoader(
           this, 
           contentUri, proj, null, null, null);        
 Cursor cursor = cursorLoader.loadInBackground();
 
 int column_index = 
   cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 
 if (cursor.isBeforeFirst())
 {
  cursor.moveToFirst();
  return cursor.getString(column_index); 
 }
 else
  return null;
}
 
Example 3
Source File: Utils.java    From Alexei with Apache License 2.0 6 votes vote down vote up
public String getRealPathFromURI(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };

    //This method was deprecated in API level 11
    //Cursor cursor = managedQuery(contentUri, proj, null, null, null);

    CursorLoader cursorLoader = new CursorLoader(
            context,
            contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    int column_index =
            cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
 
Example 4
Source File: FileHelper.java    From reader with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    try {
        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
    } catch (Exception e) {
        result = null;
    }
    return result;
}
 
Example 5
Source File: FileHelper.java    From reader with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    try {
        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
    } catch (Exception e) {
        result = null;
    }
    return result;
}
 
Example 6
Source File: FileHelper.java    From reader with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    try {
        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
    } catch (Exception e) {
        result = null;
    }
    return result;
}
 
Example 7
Source File: FileHelper.java    From reader with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    try {
        CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
    } catch (Exception e) {
        result = null;
    }
    return result;
}
 
Example 8
Source File: FileUtils.java    From KJFrameForAndroid with 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 9
Source File: FileUtils.java    From VideoMeeting with 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 10
Source File: FileUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 把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 11
Source File: RealPathUtil.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = {Images.Media.DATA};
    String result = null;

    CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
        cursor.close();
    }
    return result;
}
 
Example 12
Source File: FileUtils.java    From EmojiChat with Apache License 2.0 5 votes vote down vote up
/**
 * 把uri转为File对象
 */
public static File uri2File(Activity aty, Uri uri) {
    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 13
Source File: FileHelper.java    From simple_share with MIT License 5 votes vote down vote up
private String getRealPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(registrar.context(), uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    String result = null;
    if (cursor != null && cursor.moveToFirst()) {
        int col_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        result = cursor.getString(col_index);
        cursor.close();
    }
    return result;
}
 
Example 14
Source File: FileUtils.java    From Common with 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
Source File: UriUtil.java    From QrCodeDemo4 with MIT License 5 votes vote down vote up
/**
 * 适配api11-api18,根据uri获取图片的绝对路径
 */
private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
    String filePath = null;
    String[] projection = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();

    if (cursor != null) {
        cursor.moveToFirst();
        filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
        cursor.close();
    }
    return filePath;
}
 
Example 16
Source File: LocalPhotosGridFragment.java    From glimmr with Apache License 2.0 5 votes vote down vote up
@Override
protected void initGridView() {
    mGridView = (GridView) mLayout.findViewById(R.id.gridview);
    mGridView.setVisibility(View.VISIBLE);
    mGridView.setMultiChoiceModeListener(this);
    mShowDetailsOverlay = false;
    String[] from = {MediaStore.MediaColumns.TITLE};
    int[] to = {android.R.id.text1};

    CursorLoader cursorLoader = new CursorLoader(
            getActivity(),
            SOURCE_URI,
            null,
            null,
            null,
            MediaStore.Audio.Media.TITLE);

    Cursor cursor = cursorLoader.loadInBackground();

    mAdapter = new MediaStoreImagesAdapter(
            getActivity(),
            R.layout.gridview_item,
            cursor,
            from,
            to);

    mGridView.setAdapter(mAdapter);
    mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        final String usageTip = getString(R.string.upload_photos_tip);
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            UsageTips.getInstance().show(mActivity, usageTip, true);
        }
    });
}
 
Example 17
Source File: FileHelper.java    From simple_share with MIT License 5 votes vote down vote up
private String getRealPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(registrar.context(), uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    String result = null;
    if (cursor != null && cursor.moveToFirst()) {
        int col_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        result = cursor.getString(col_index);
        cursor.close();
    }
    return result;
}
 
Example 18
Source File: UriUtil.java    From vmqApk with MIT License 5 votes vote down vote up
/**
 * 适配api11-api18,根据uri获取图片的绝对路径
 */
private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
    String filePath = null;
    String[] projection = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();

    if (cursor != null) {
        cursor.moveToFirst();
        filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
        cursor.close();
    }
    return filePath;
}
 
Example 19
Source File: VideoSelectorActivity.java    From imsdk-android with MIT License 4 votes vote down vote up
public void initData() {
    final String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
            MediaStore.Video.Thumbnails.VIDEO_ID};

    String[] projection = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION,MediaStore.Video.Media.DATE_ADDED};
    CursorLoader loader = new CursorLoader(VideoSelectorActivity.this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
            null, // Return all rows
            null, MediaStore.Video.Media.DATE_ADDED + " DESC");
    videos = new ArrayList();
    final Cursor mCursor = loader.loadInBackground();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (mCursor.moveToNext()) {
                String id = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media._ID));
                String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA));
                String size = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.SIZE));
                String duration = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DURATION));

                LogUtil.d(TAG, path);
                VideoInfo info = new VideoInfo();

                Cursor thumbCursor = VideoSelectorActivity.this.getContentResolver().query(
                        MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                        thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
                                + "=" + id, null, null);
                if (thumbCursor != null && thumbCursor.moveToFirst()) {
                    info.thumbPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                }


                info.path = path;
                if(!TextUtils.isEmpty(size)){
                    info.size = size;
                }
                if(!TextUtils.isEmpty(duration)){
                    info.duration = duration;
                }
                videos.add(info);
            }
            mCursor.close();
            handler.sendEmptyMessage(0);
        }
    }).start();
}
 
Example 20
Source File: LocalPhotosGridFragment.java    From glimmr with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(final int position, View convertView,
        ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mActivity.getLayoutInflater().inflate(
                R.layout.gridview_item, null);
        holder = new ViewHolder();
        holder.imageOverlay = (LinearLayout) convertView.findViewById(R.id.imageOverlay);
        holder.image = (ImageView) convertView.findViewById(R.id.image_item);
        holder.imageNewRibbon = (ImageView) convertView.findViewById(R.id.imageNewRibbon);
        holder.ownerText = (TextView) convertView.findViewById(R.id.ownerText);
        holder.viewsText = (TextView) convertView.findViewById(R.id.viewsText);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.imageOverlay.setVisibility(View.INVISIBLE);

    mCursor.moveToPosition(position);

    int myID = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media._ID));

    String[] thumbColumns = {THUMB_DATA, THUMB_IMAGE_ID};
    CursorLoader thumbCursorLoader = new CursorLoader(
            getActivity(),
            THUMB_URI,
            thumbColumns,
            THUMB_IMAGE_ID + "=" + myID,
            null,
            null);
    Cursor thumbCursor = thumbCursorLoader.loadInBackground();

    Bitmap myBitmap = null;
    if(thumbCursor.moveToFirst()){
        int thCulumnIndex = thumbCursor.getColumnIndex(THUMB_DATA);
        String thumbPath = thumbCursor.getString(thCulumnIndex);
        myBitmap = BitmapFactory.decodeFile(thumbPath);
        holder.image.setImageBitmap(myBitmap);
    }

    /* Set tint on selected items */
    SparseBooleanArray checkArray = mGridView.getCheckedItemPositions();
    if (checkArray.get(position)) {
        int highlightColor = mActivity.getResources().getColor(
                R.color.transparent_flickr_pink);
        holder.image.setColorFilter(highlightColor);
    } else {
        holder.image.setColorFilter(null);
    }

    return convertView;
}