我使用下面提供的代碼從其路徑獲取視頻文件ID。但它是拋出異常。我可以從它的路徑獲取視頻文件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):at android.database.AbstractCursor.checkPosition E/ACRA(24407):at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:147) 05-04 19:45:34.089:E/ACRA(24407) ACRA(24407):at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:85) 05-04 19:45:34.089:E/ACRA(24407):a t android.database.CursorWrapper.getLong(CursorWrapper.java:106) 05-04 19:45:34.089:E/ACRA(24407):at 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):at net.video.bunty.cutvideopreview.CutVideoPreviewActivity $ 1.surfaceCreated(CutVideoPreviewActivity.java:76) 05-04 19:45:34.089:E/ACRA(24407):at android.view.SurfaceView.updateWindow (SurfaceView.java:662) 05-04 19:45:34.089:E/ACRA(24407):at 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):at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2197) 05-04 19:45:34.089:E/ACRA(24407):at 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):at android.view.Choreographer.doCallbacks(Choreographer.java:591) 05-04 19/45:34.089:E/ACRA(24407):at android.view.Choreographer.doFrame(Choreographer.java:560) 05-04 19:45:34.089:E/ACRA(244 07):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:774) 05-04 19:45:34.089:E/ACRA(24407):at android.os.Handler.handleCallback(Handler.java: 808) 05-04 19:45:34.089:E/ACRA(24407):at android.os.Handler.dispatchMessage(Handler.java:103) 05-04 19:45:34.089:E/ACRA(24407) :at android.os.Looper.loop(Looper.java:193) 05-04 19:45:34.089:E/ACRA(24407):at android.app.ActivityThread.main(ActivityThread.java:5292) 05 -04 19:45:34.089:E/ACRA(24407):at java.lang.reflect.Method.invokeNative(Native Method) 05-04 19:45:34.089:E/ACRA(24407):at java.lang .reflect.Method.invoke(Method.java:515) 05-04 19:45:34.089:E/ACRA(24407):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828 ) 05-0 4 19:45:34.089:E/ACRA(24407):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 05-04 19:45:34.089:E/ACRA(24407)在dalvik.system.NativeStart。主(本地方法)
沒有保證做這種轉換的方法。除此之外,您需要發佈Java堆棧跟蹤以幫助您處理異常。 – CommonsWare
它引發CursorIndexOutOfBoundsException –