Java Code Examples for android.provider.MediaStore.MediaColumns#DATA

The following examples show how to use android.provider.MediaStore.MediaColumns#DATA . 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: ImageUtils.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取图片路径
 *
 * @param uri
 * @param
 */
public static String getImagePath(Uri uri, Activity context) {

    String[] projection = {MediaColumns.DATA};
    Cursor cursor = context.getContentResolver().query(uri, projection,
            null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        String ImagePath = cursor.getString(columIndex);
        cursor.close();
        return ImagePath;
    }

    return uri.toString();
}
 
Example 2
Source File: Utils.java    From android-utils with MIT License 6 votes vote down vote up
/**
 * Get the file path from the Media Content Uri for video, audio or images.
 *
 * @param mediaContentUri Media content Uri.
 **/
public static String getPathForMediaUri(Context context, Uri mediaContentUri) {

    Cursor cur = null;
    String path = null;

    try {
        String[] projection = {MediaColumns.DATA};
        cur = context.getContentResolver().query(mediaContentUri, projection, null, null, null);

        if (cur != null && cur.getCount() != 0) {
            cur.moveToFirst();
            path = cur.getString(cur.getColumnIndexOrThrow(MediaColumns.DATA));
        }

        // Log.v( TAG, "#getRealPathFromURI Path: " + path );
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cur != null && !cur.isClosed())
            cur.close();
    }

    return path;
}
 
Example 3
Source File: UriFileUtils.java    From Overchan-Android with GNU General Public License v3.0 6 votes vote down vote up
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = MediaColumns.DATA;
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}
 
Example 4
Source File: ImageUtils.java    From monolog-android with MIT License 5 votes vote down vote up
public static String getImagePath(Uri uri, Activity context) {

        String[] projection = {MediaColumns.DATA};
        Cursor cursor = context.getContentResolver().query(uri, projection,
                null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            String ImagePath = cursor.getString(columIndex);
            cursor.close();
            return ImagePath;
        }

        return uri.toString();
    }
 
Example 5
Source File: MediaUtils.java    From memoir with Apache License 2.0 5 votes vote down vote up
private static String getPathFromUri(Context context, Uri imageUri) {
    String filePath = "";

    if (imageUri.toString().startsWith("content://com.android.gallery3d.provider")) {
        imageUri = Uri.parse(imageUri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
    }

    Cursor cursor = null;
    try {
        String column = MediaColumns.DATA;
        String[] proj = {MediaColumns.DATA};

        cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
        cursor.moveToFirst();

        if (imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
            filePath = imageUri.toString();
        } else {
            filePath = cursor.getString(cursor.getColumnIndexOrThrow(column));
        }
    } catch (Exception ignore) {
        // Google Drive content provider throws an exception that we ignore
        // content://com.google.android.apps.docs.storage
    } finally {
        Helper.closeQuietly(cursor);
    }

    if (isNullOrEmpty(filePath) || !new File(filePath).exists() ||
            imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
        filePath = imageUri.toString();
    }

    return filePath;
}
 
Example 6
Source File: MediaUtils.java    From memoir with Apache License 2.0 5 votes vote down vote up
private static String getPathFromUri(Context context, Uri imageUri) {
    String filePath = "";

    if (imageUri.toString().startsWith("content://com.android.gallery3d.provider")) {
        imageUri = Uri.parse(imageUri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
    }

    Cursor cursor = null;
    try {
        String column = MediaColumns.DATA;
        String[] proj = {MediaColumns.DATA};

        cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
        cursor.moveToFirst();

        if (imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
            filePath = imageUri.toString();
        } else {
            filePath = cursor.getString(cursor.getColumnIndexOrThrow(column));
        }
    } catch (Exception ignore) {
        // Google Drive content provider throws an exception that we ignore
        // content://com.google.android.apps.docs.storage
    } finally {
        Helper.closeQuietly(cursor);
    }

    if (isNullOrEmpty(filePath) || !new File(filePath).exists() ||
            imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
        filePath = imageUri.toString();
    }

    return filePath;
}
 
Example 7
Source File: MusicUtils.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
/**
 * @param context
 * @param id
 */
public static void setRingtone(Context context, long id) {
    ContentResolver resolver = context.getContentResolver();
    // Set the flag in the database to mark this as a ringtone
    Uri ringUri = ContentUris.withAppendedId(Media.EXTERNAL_CONTENT_URI, id);
    try {
        ContentValues values = new ContentValues(2);
        values.put(AudioColumns.IS_RINGTONE, "1");
        values.put(AudioColumns.IS_ALARM, "1");
        resolver.update(ringUri, values, null, null);
    } catch (UnsupportedOperationException ex) {
        // most likely the card just got unmounted
        return;
    }

    String[] cols = new String[] {
            BaseColumns._ID, MediaColumns.DATA, MediaColumns.TITLE
    };

    String where = BaseColumns._ID + "=" + id;
    Cursor cursor = query(context, Media.EXTERNAL_CONTENT_URI, cols, where, null, null);
    try {
        if (cursor != null && cursor.getCount() == 1) {
            // Set the system setting to make this the current ringtone
            cursor.moveToFirst();
            Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
            String message = context.getString(R.string.set_as_ringtone, cursor.getString(2));
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 8
Source File: MediaUtils.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
private static String getPathFromUri(Context context, Uri imageUri) {
    String filePath = "";

    if (imageUri.toString().startsWith("content://com.android.gallery3d.provider")) {
        imageUri = Uri.parse(imageUri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
    }

    Cursor cursor = null;
    try {
        String column = MediaColumns.DATA;
        String[] proj = {MediaColumns.DATA};

        cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
        cursor.moveToFirst();

        if (imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
            filePath = imageUri.toString();
        } else {
            filePath = cursor.getString(cursor.getColumnIndexOrThrow(column));
        }
    } catch (Exception ignore) {
        // Google Drive content provider throws an exception that we ignore
        // content://com.google.android.apps.docs.storage
    } finally {
        Helper.closeQuietly(cursor);
    }

    if (isNullOrEmpty(filePath) || !new File(filePath).exists() ||
            imageUri.toString().startsWith("content://com.google.android.gallery3d")) {
        filePath = imageUri.toString();
    }

    return filePath;
}
 
Example 9
Source File: EditorActivity.java    From RichEditText with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定uri的本地绝对路径
 * 
 * @param uri
 * @return
 */
@SuppressWarnings("deprecation")
protected String getAbsoluteImagePath(Uri uri) {
	// can post image
	String[] proj = { MediaColumns.DATA };
	Cursor cursor = 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)

	int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
	cursor.moveToFirst();

	return cursor.getString(column_index);
}
 
Example 10
Source File: ImageHelper.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
public static Uri getContentUriFromFile(final Context ctx,
        final File imageFile) {
    Uri uri = null;
    final ContentResolver cr = ctx.getContentResolver();
    // Columns to return
    final String[] projection = { BaseColumns._ID, MediaColumns.DATA };
    // Look for a picture which matches with the requested path
    // (MediaStore stores the path in column Images.Media.DATA)
    final String selection = MediaColumns.DATA + " = ?";
    final String[] selArgs = { imageFile.toString() };

    final Cursor cursor = cr.query(Images.Media.EXTERNAL_CONTENT_URI,
            projection, selection, selArgs, null);

    if (cursor.moveToFirst()) {

        String id;
        final int idColumn = cursor.getColumnIndex(BaseColumns._ID);
        id = cursor.getString(idColumn);
        uri = Uri.withAppendedPath(Images.Media.EXTERNAL_CONTENT_URI, id);
    }
    cursor.close();
    if (uri != null) {
        Log.d(ImageHelper.TAG,
                "Found picture in MediaStore : " + imageFile.toString()
                        + " is " + uri.toString());
    } else {
        Log.d(ImageHelper.TAG, "Did not find picture in MediaStore : "
                + imageFile.toString());
    }
    return uri;
}
 
Example 11
Source File: VideoPicturePickerActivity.java    From cordova-plugin-video-picture-preview-picker-V2 with MIT License 4 votes vote down vote up
private void getData() {

        data = new ArrayList<ImageOrVideoItem>();
        Uri imagesUri, videoUri;
        Cursor imageCursor, videoCursor;
        String imageAbsolutePath;
        int image_column_index_data, image_title,

                column_index_data_FOR_VIDEO, image_date_insert, video_date_insert, video_title, video_description;

        imagesUri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] imageProjection =
                {
                        MediaColumns.DATA,
                        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                        MediaStore.Images.Media.DATE_ADDED,
                        MediaStore.Images.Media.MIME_TYPE
                };


        imageCursor = this.getContentResolver().query(imagesUri, imageProjection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
        MIME_TYPE = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE);
        image_column_index_data = imageCursor.getColumnIndexOrThrow(MediaColumns.DATA);
        image_title = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        image_date_insert = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED);


        String[] videoProjection =
                {
                        MediaStore.Video.VideoColumns.DATA,
                        MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
                        MediaStore.Video.VideoColumns.DURATION,
                        MediaStore.Video.VideoColumns.DATE_ADDED
                };

        videoUri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        videoCursor = this.getContentResolver().query(videoUri, videoProjection, null, null, null);
        column_index_data_FOR_VIDEO = videoCursor.getColumnIndexOrThrow(MediaColumns.DATA);
        video_date_insert = videoCursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DATE_ADDED);
        video_title = videoCursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME);
        video_description = videoCursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION);
        if (videoCursor.getColumnCount() > 0 && video_selector) {
            while (videoCursor.moveToNext()) {
                String videoPath = videoCursor.getString(column_index_data_FOR_VIDEO);

                ImageOrVideoItem video = new ImageOrVideoItem
                        (null,
                                videoCursor.getString(video_title),
                                videoCursor.getString(video_description),
                                videoPath,
                                true,
                                Long.parseLong(videoCursor.getString(video_date_insert))
                        );

                data.add(video);
            }

            videoCursor.close();
        }

        if (imageCursor.getColumnCount() > 0 && picture_selector) {

            while (imageCursor.moveToNext()) {
                //gif pictures will cause problem in preview
                if (imageCursor.getString(MIME_TYPE).toLowerCase().contains("image/gif"))
                    continue;

                //**********************************************************************************
                // let's calculate from how long the picture is token
                //**********************************************************************************
                long time = Long.parseLong(imageCursor.getString(image_date_insert));
                long now = System.currentTimeMillis();
                CharSequence relativeTimeStr = DateUtils.getRelativeTimeSpanString(time * 1000, now,
                        DateUtils.SECOND_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
                //**********************************************************************************

                imageAbsolutePath = imageCursor.getString(image_column_index_data);
                data.add(new ImageOrVideoItem(
                        null,
                        imageCursor.getString(image_title),
                        (String) relativeTimeStr,
                        imageAbsolutePath,
                        false,
                        time)
                );


            }

            imageCursor.close();
        }

        Collections.sort(data, ImageOrVideoItem.getCompByName());


    }
 
Example 12
Source File: ApolloService.java    From mobile-manager-tool with MIT License 4 votes vote down vote up
/**
 * Opens the specified file and readies it for playback.
 *
 * @param path The full path of the file to be opened.
 */
public boolean open(String path) {
    synchronized (this) {
        if (path == null) {
            return false;
        }

        // if mCursor is null, try to associate path with a database cursor
        if (mCursor == null) {

            ContentResolver resolver = getContentResolver();
            Uri uri;
            String where;
            String selectionArgs[];
            if (path.startsWith("content://media/")) {
                uri = Uri.parse(path);
                where = null;
                selectionArgs = null;
            } else {
                // Remove schema for search in the database
                // Otherwise the file will not found
                String data = path;
                if( data.startsWith("file://") ){
                    data = data.substring(7);
                }
                uri = Audio.Media.getContentUriForPath(path);
                where = MediaColumns.DATA + "=?";
                selectionArgs = new String[] {
                    data
                };
            }

            try {
                mCursor = resolver.query(uri, mCursorCols, where, selectionArgs, null);
                if (mCursor != null) {
                    if (mCursor.getCount() == 0) {
                        mCursor.close();
                        mCursor = null;
                    } else {
                        mCursor.moveToNext();
                        ensurePlayListCapacity(1);
                        mPlayListLen = 1;
                        mPlayList[0] = mCursor.getLong(IDCOLIDX);
                        mPlayPos = 0;
                    }
                }
            } catch (UnsupportedOperationException ex) {
            }

            updateAlbumBitmap();
        }
        mFileToPlay = path;
        mPlayer.setDataSource(mFileToPlay);
        if (mPlayer.isInitialized()) {
            mOpenFailedCounter = 0;
            return true;
        }
        stop(true);
        return false;
    }
}
 
Example 13
Source File: NowPlayingFragment.java    From mobile-manager-tool with MIT License 4 votes vote down vote up
/**
   * Reload the queue after we remove a track
   */
  private void reloadQueueCursor() {
      String[] cols = new String[] {
              BaseColumns._ID, MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM,
              AudioColumns.ARTIST, AudioColumns.ARTIST_ID
      };
      StringBuilder selection = new StringBuilder();
      selection.append(AudioColumns.IS_MUSIC + "=1");
      selection.append(" AND " + MediaColumns.TITLE + " != ''");
      Uri uri = Audio.Media.EXTERNAL_CONTENT_URI;
      long[] mNowPlaying = MusicUtils.getQueue();
      if (mNowPlaying.length == 0) {
      }
      selection = new StringBuilder();
      selection.append(BaseColumns._ID + " IN (");
      for (int i = 0; i < mNowPlaying.length; i++) {
          selection.append(mNowPlaying[i]);
          if (i < mNowPlaying.length - 1) {
              selection.append(",");
          }
      }
      selection.append(")");
if(mCursor != null)
	mCursor.close();
      mCursor = MusicUtils.query(getActivity(), uri, cols, selection.toString(), null, null);
      String[] audioCols = new String[] { BaseColumns._ID, MediaColumns.TITLE, AudioColumns.ARTIST, AudioColumns.ALBUM}; 
      MatrixCursor playlistCursor = new MatrixCursor(audioCols);
  	for(int i = 0; i < mNowPlaying.length; i++){
  		mCursor.moveToPosition(-1);
  		while (mCursor.moveToNext()) {
              long audioid = mCursor.getLong(mCursor.getColumnIndexOrThrow(BaseColumns._ID));
          	if( audioid == mNowPlaying[i]) {
                  String trackName = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));
                  String artistName = mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ARTIST));
                  String albumName = mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ALBUM));
          		playlistCursor.addRow(new Object[] {audioid, trackName, artistName ,albumName});

          	}
          }
  	}
if(mCursor != null)
	mCursor.close();
      mCursor = playlistCursor;
      mAdapter.changeCursor(playlistCursor);
  }