Android视频文件路径

时间:2013-12-03 16:19:28

标签: android video android-asynctask

我有一些代码据称可以在Async类中查找以前选择的视频的路径。我搜索过SO并且所有解决方案都是相似的,如果不相同,但我获得的路径始终为null,当我打印它时,应用程序崩溃。

代码如下:

 // The file location of the image selected.
            Uri selectedVideo = uris[0];

            String[] filePathColumn = { MediaStore.Video.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedVideo, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);

            Log.i("INFO", filePath);
            cursor.close();

日志错误:

12-03 16:47:51.069    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:48:33.884    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:48:48.130  15711-31523/? E/JHEAD﹕ can't open '/0/file:///data/data/com.google.android.apps.plus/cache/media_sync/6/6aba39805abcb82-l'
12-03 16:48:48.140  15711-31523/? E/JHEAD﹕ can't open '/0/file:///data/data/com.google.android.apps.plus/cache/media_sync/f/f33ffcaf70cce9-l'
12-03 16:48:50.182      580-649/? E/ConnectivityService﹕ Unexpected mtu value: android.net.wifi.WifiStateTracker@42390b40
12-03 16:48:53.485  31630-31630/? E/PhoneMonitor﹕ onOtaspChanged old =0, new =3
12-03 16:48:58.581    176-31758/? E/AudioSink﹕ received unknown event type: 1 inside CallbackWrapper !
12-03 16:48:58.601    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:49:16.510    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:49:16.510    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:49:16.640    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:49:16.640    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:49:26.010    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:49:42.748    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:49:53.629    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:50:04.301    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:50:19.137    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:19.137    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:21.319    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:21.319    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:21.970    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:21.970    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:25.233    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:25.233    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:50:28.997    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:50:38.718    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:50:47.627    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:50:53.473    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:51:07.108    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:51:18.110    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:18.110    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:18.500    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:18.500    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:22.054    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:51:40.073    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:51:47.761    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:47.761    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:48.282    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:48.282    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:51:50.824    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:52:13.869    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:52:17.243    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:52:29.576    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:29.576    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:29.736    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:29.736    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:29.956    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:29.956    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:30.206    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:52:30.206    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:54:45.391    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:54:45.391    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:54:45.681    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:54:45.681    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:54:45.711    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:54:53.630    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:55:03.260    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:55:23.351    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:55:23.351    2644-2644/? E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
12-03 16:55:25.373    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:55:39.408    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:55:41.240    2644-2644/? E/MediaPlayer﹕ Should have subtitle controller already set
12-03 16:57:47.005  32440-32440/? E/dalvikvm﹕ Could not find class 'com.google.trix.ritz.shared.messages.b', referenced from method bgi.<init>
12-03 16:57:47.005  32440-32440/? E/dalvikvm﹕ Could not find class 'com.google.trix.ritz.shared.render.g', referenced from method bgi.<init>
12-03 16:57:47.015  32440-32440/? E/dalvikvm﹕ Could not find class 'com.google.trix.ritz.shared.behavior.validation.a', referenced from method bgi.<init>
12-03 16:57:47.015  32440-32440/? E/dalvikvm﹕ Could not find class 'com.google.trix.ritz.shared.mutation.N', referenced from method bgi.<init>
12-03 16:57:48.226  32521-32521/? E/MY KEY HASH:﹕ PaXGYdYcmTsLk5ibvJIlKJeOkQ8=
12-03 16:57:51.559  32470-32605/com.amazonaws.demo.s3uploader E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.amazonaws.demo.s3uploader, PID: 32470
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException: println needs a message
            at android.util.Log.println_native(Native Method)
            at android.util.Log.i(Log.java:160)
            at com.amazonaws.demo.s3uploader.S3UploaderActivity$S3PutObjectTask.doInBackground(S3UploaderActivity.java:155)
            at com.amazonaws.demo.s3uploader.S3UploaderActivity$S3PutObjectTask.doInBackground(S3UploaderActivity.java:123)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
12-03 16:57:52.060  32470-32470/com.amazonaws.demo.s3uploader E/WindowManager﹕ android.view.WindowLeaked: Activity com.amazonaws.demo.s3uploader.S3UploaderActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41ff97f8 V.E..... R.....ID 0,0-357,230} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:286)
            at com.amazonaws.demo.s3uploader.S3UploaderActivity$S3PutObjectTask.onPreExecute(S3UploaderActivity.java:131)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
            at android.os.AsyncTask.execute(AsyncTask.java:535)
            at com.amazonaws.demo.s3uploader.S3UploaderActivity.onActivityResult(S3UploaderActivity.java:79)
            at android.app.Activity.dispatchActivityResult(Activity.java:5435)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3342)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389)
            at android.app.ActivityThread.access$1200(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

我错过了什么?

1 个答案:

答案 0 :(得分:3)

修改

我确信您的代码可以在4.3及更低版本上运行,但KitKat有一些变化。希望这些主题能够提供帮助:

Get real path from URI, Android KitKat new storage access framework

Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT


您的代码看起来是正确的。尝试:

String[] filePathColumn = { MediaStore.Video.VideoColumns.Data };

并且,也许,您应该更改您的查询:

Cursor cursor = getContentResolver().query(selectedVideo, null, null, null, null);

最后,你的URI是什么?它可能是Dropbox或其他类似的本地文件路径。