【问题标题】:Getting video file id from its path?从其路径获取视频文件ID?
【发布时间】:2015-05-04 22:16:44
【问题描述】:

我正在使用下面提供的代码从其路径获取视频文件 ID。但它正在抛出异常。我怎样才能从其路径中获取视频文件 ID。

private long getVideoIdFromFilePath(String filePath,
        ContentResolver contentResolver) {


    long videoId;
    //Log.d(TAG,"Loading file " + filePath);

            // This returns us content://media/external/videos/media (or something like that)
            // I pass in "external" because that's the MediaStore's name for the external
            // storage on my device (the other possibility is "internal")
    Uri videosUri = MediaStore.Video.Media.getContentUri("internal");

   // Log.d(TAG,"videosUri = " + videosUri.toString());

    String[] projection = {MediaStore.Video.VideoColumns._ID};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(projection[0]);
    videoId = cursor.getLong(columnIndex);

    //Log.d(TAG,"Video ID is " + videoId);
    cursor.close();
    return videoId;
}

05-04 19:45:34.089: E/ACRA(24407): com.pcs.sliderringtineproj 致命错误:请求索引 -1,大小为 0 05-04 19:45:34.089: E/ACRA(24407): android.database.CursorIndexOutOfBoundsException: 请求索引 -1,大小为 0 05-04 19:45:34.089: E/ACRA(24407): 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 05-04 19:45:34.089: E/ACRA(24407): 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:147) 05-04 19:45:34.089: E/ACRA(24407): 在 android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:85) 05-04 19:45:34.089: E/ACRA(24407): 在 android.database.CursorWrapper.getLong(CursorWrapper.java:106) 05-04 19:45:34.089: E/ACRA(24407): 在 net.video.bunty.cutvideopreview.CutVideoPreviewActivity.getVideoIdFromFilePath(CutVideoPreviewActivity.java:237) 05-04 19:45:34.089: E/ACRA(24407): at net.video.bunty.cutvideopreview.CutVideoPreviewActivity.access$0(CutVideoPreviewActivity.java:216) 05-04 19:45:34.089: E/ACRA(24407): 在 net.video.bunty.cutvideopreview.CutVideoPreviewActivity$1.surfaceCreated(CutVideoPreviewActivity.java:76) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.SurfaceView.updateWindow(SurfaceView.java:662) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.SurfaceView.access$000(SurfaceView.java:90) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.SurfaceView$3.onPreDraw(SurfaceView.java:195) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2197) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.Choreographer.doCallbacks(Choreographer.java:591) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.Choreographer.doFrame(Choreographer.java:560) 05-04 19:45:34.089: E/ACRA(24407): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) 05-04 19:45:34.089: E/ACRA(24407): 在 android.os.Handler.handleCallback(Handler.java:808) 05-04 19:45:34.089: E/ACRA(24407): 在 android.os.Handler.dispatchMessage(Handler.java:103) 05-04 19:45:34.089: E/ACRA(24407): 在 android.os.Looper.loop(Looper.java:193) 05-04 19:45:34.089: E/ACRA(24407): 在 android.app.ActivityThread.main(ActivityThread.java:5292) 05-04 19:45:34.089: E/ACRA(24407): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-04 19:45:34.089: E/ACRA(24407): 在 java.lang.reflect.Method.invoke(Method.java:515) 05-04 19:45:34.089: E/ACRA(24407): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 05-04 19:45:34.089: E/ACRA(24407): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 05-04 19:45:34.089: E/ACRA(24407): at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 没有保证进行此转换的方法。除此之外,您还需要为我们发布 Java 堆栈跟踪以帮助您解决异常。
  • 它抛出 CursorIndexOutOfBoundsException

标签: android mediastore


【解决方案1】:

它抛出 CursorIndexOutOfBoundsException

您的query() 正在返回一个没有行的Cursor,因此moveToFirst() 将不起作用。

您需要:

  • 通过getCount()检查Cursor是否有行,并处理查询没有返回结果的情况,或者

  • 不要这样做,因为不需要通过某种方式将某些“filePath”转换为视频 ID。如果您需要视频 ID,请从 MediaStore 检索视频本身并获取其 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2016-10-12
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多