2016-04-27 93 views
1

我無法嘗試在Android上選擇視頻。我正在使用下面的代碼來啓動該活動。從圖庫中挑選視頻時,應用程序崩潰

var intent = new Intent(); 
      intent.SetType("video/*"); 
      intent.SetAction(Intent.ActionGetContent); 
      this.StartActivityForResult(Intent.CreateChooser(intent, "Select video"), 200); 

然後在OnActivityResult回調中,我使用下面的代碼來獲取數據的URI。

public override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
    { 
     base.OnActivityResult(requestCode, resultCode, data); 

     if (requestCode == 200 && resultCode == Result.Ok) 
     { 
      try 
      { 
       Android.Net.Uri contentURI = data.Data; 

       ICursor cursor = Activity.ContentResolver.Query(contentURI, null, null, null, null); 
       cursor.MoveToFirst(); 
       string documentId = cursor.GetString(0); 
       documentId = documentId.Split(':')[1]; 
       cursor.Close(); 

       cursor = Activity.ContentResolver.Query(
       Android.Provider.MediaStore.Video.Media.ExternalContentUri, 
       null, MediaStore.Video.Media.InterfaceConsts.Id + " = ? ", new[] { documentId }, null); 
       cursor.MoveToFirst(); 
       string selectedPath = cursor.GetString(cursor.GetColumnIndex(MediaStore.Video.Media.InterfaceConsts.Data)); 

       var fileBytes = System.IO.File.ReadAllBytes(selectedPath); 

       //await UploadFile(fileBytes); 

      } 
      catch (Java.Lang.Exception e) 
      { 
       System.Console.WriteLine(e.Message); 
      } 
     } 
    } 

該應用程序在圖庫打開後不久崩潰。我使用Android版本4.0.3的Sony Xperia ion來調試應用程序。我第一次測試我的代碼時,它運行正常,但現在每次圖庫打開時,應用程序都會崩潰。

這裏是我的全部的logcat:

4-28 02:15:18.102 D/Mono (10277): DllImport attempting to load: '__Internal'. 
04-28 02:15:18.102 D/Mono (10277): DllImport loaded library '(null)'. 
04-28 02:15:18.102 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.102 D/Mono (10277): Searching for '_monodroid_lref_log_new'. 
04-28 02:15:18.102 D/Mono (10277): Probing '_monodroid_lref_log_new'. 
04-28 02:15:18.102 D/Mono (10277): Found as '_monodroid_lref_log_new'. 
04-28 02:15:18.112 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.112 D/Mono (10277): Searching for '_monodroid_lref_log_delete'. 
04-28 02:15:18.112 D/Mono (10277): Probing '_monodroid_lref_log_delete'. 
04-28 02:15:18.112 D/Mono (10277): Found as '_monodroid_lref_log_delete'. 
04-28 02:15:18.112 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.112 D/Mono (10277): Searching for '_monodroid_gref_log_new'. 
04-28 02:15:18.112 D/Mono (10277): Probing '_monodroid_gref_log_new'. 
04-28 02:15:18.112 D/Mono (10277): Found as '_monodroid_gref_log_new'. 
04-28 02:15:18.142 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.142 D/Mono (10277): Searching for '_monodroid_get_identity_hash_code'. 
04-28 02:15:18.142 D/Mono (10277): Probing '_monodroid_get_identity_hash_code'. 
04-28 02:15:18.142 D/Mono (10277): Found as '_monodroid_get_identity_hash_code'. 
04-28 02:15:18.263 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.263 D/Mono (10277): Searching for 'monodroid_typemap_java_to_managed'. 
04-28 02:15:18.263 D/Mono (10277): Probing 'monodroid_typemap_java_to_managed'. 
04-28 02:15:18.263 D/Mono (10277): Found as 'monodroid_typemap_java_to_managed'. 
04-28 02:15:18.413 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.413 D/Mono (10277): Searching for '_monodroid_gref_log'. 
04-28 02:15:18.413 D/Mono (10277): Probing '_monodroid_gref_log'. 
04-28 02:15:18.413 D/Mono (10277): Found as '_monodroid_gref_log'. 
04-28 02:15:18.453 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:18.453 D/Mono (10277): Searching for 'monodroid_typemap_managed_to_java'. 
04-28 02:15:18.453 D/Mono (10277): Probing 'monodroid_typemap_managed_to_java'. 
04-28 02:15:18.453 D/Mono (10277): Found as 'monodroid_typemap_managed_to_java'. 
04-28 02:15:18.503 D/Mono (10277): Image addref System.Core[0xe692e8] -> System.Core.dll[0xe77a50]: 1 
04-28 02:15:18.503 D/Mono (10277): Assembly System.Core[0xe692e8] added to domain RootDomain, ref_count=1 
04-28 02:15:18.503 D/Mono (10277): AOT module 'System.Core.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Core.dll.so' not found 
04-28 02:15:18.503 D/Mono (10277): AOT module '/Users/builder/data/lanes/3053/a94a03b5/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Core.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Core.dll.so' not found 
04-28 02:15:18.523 D/Mono (10277): Unloading image data-0x63529008 [0xe86468]. 
04-28 02:15:18.523 D/Mono (10277): Assembly Ref addref Mono.Android[0xca8958] -> System.Core[0xe692e8]: 2 
04-28 02:15:18.533 D/Mono (10277): Assembly Ref addref System.Core[0xe692e8] -> mscorlib[0xc76d38]: 3 
Loaded assembly: System.Core.dll 
04-28 02:15:19.123 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:19.123 D/Mono (10277): Searching for '_monodroid_gref_log_delete'. 
04-28 02:15:19.123 D/Mono (10277): Probing '_monodroid_gref_log_delete'. 
04-28 02:15:19.123 D/Mono (10277): Found as '_monodroid_gref_log_delete'. 
04-28 02:15:19.143 D/Mono (10277): DllImport searching in: '__Internal' ('(null)'). 
04-28 02:15:19.143 D/Mono (10277): Searching for '_monodroid_gc_wait_for_bridge_processing'. 
04-28 02:15:19.143 D/Mono (10277): Probing '_monodroid_gc_wait_for_bridge_processing'. 
04-28 02:15:19.143 D/Mono (10277): Found as '_monodroid_gc_wait_for_bridge_processing'. 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref OkHttp[0xc5e660] -> mscorlib[0xc76d38]: 4 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref OkHttp[0xc5e660] -> Mono.Android[0xca8958]: 2 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Refractored.Controls.CircleImageView[0xbe5e30] -> mscorlib[0xc76d38]: 5 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Refractored.Controls.CircleImageView[0xbe5e30] -> Mono.Android[0xca8958]: 3 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Animated.Vector.Drawable[0xc392e0] -> mscorlib[0xc76d38]: 6 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Animated.Vector.Drawable[0xc392e0] -> Mono.Android[0xca8958]: 4 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Design[0xc38300] -> mscorlib[0xc76d38]: 7 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Design[0xc38300] -> Mono.Android[0xca8958]: 5 
04-28 02:15:19.173 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v4[0xbe7ef8] -> mscorlib[0xc76d38]: 8 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v4[0xbe7ef8] -> Mono.Android[0xca8958]: 6 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.AppCompat[0xb7ff80] -> mscorlib[0xc76d38]: 9 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.AppCompat[0xb7ff80] -> Mono.Android[0xca8958]: 7 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.CardView[0xbdea28] -> mscorlib[0xc76d38]: 10 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.CardView[0xbdea28] -> Mono.Android[0xca8958]: 8 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.RecyclerView[0xc2b0d0] -> mscorlib[0xc76d38]: 11 
04-28 02:15:19.183 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.v7.RecyclerView[0xc2b0d0] -> Mono.Android[0xca8958]: 9 
04-28 02:15:19.193 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Vector.Drawable[0xc22340] -> mscorlib[0xc76d38]: 12 
04-28 02:15:19.193 D/Mono (10277): Assembly Ref addref Xamarin.Android.Support.Vector.Drawable[0xc22340] -> Mono.Android[0xca8958]: 10 
04-28 02:15:19.193 D/Mono (10277): Assembly Ref addref Yourtime[0xc28778] -> Mono.Android[0xca8958]: 11 
Loaded assembly: MonoDroidConstructors [External] 
04-28 02:15:19.444 D/Mono (10277): Assembly Ref addref Yourtime[0xc28778] -> mscorlib[0xc76d38]: 13 
04-28 02:15:20.234 D/Mono (10277): Image addref System.Net.Http[0x1255c38] -> System.Net.Http.dll[0x1256b70]: 1 
04-28 02:15:20.234 D/Mono (10277): Assembly System.Net.Http[0x1255c38] added to domain RootDomain, ref_count=1 
04-28 02:15:20.234 D/Mono (10277): AOT module 'System.Net.Http.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Net.Http.dll.so' not found 
04-28 02:15:20.234 D/Mono (10277): AOT module '/Users/builder/data/lanes/3053/a94a03b5/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Net.Http.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Net.Http.dll.so' not found 
04-28 02:15:20.234 D/Mono (10277): Unloading image data-0x125e3e8 [0x1256fc0]. 
04-28 02:15:20.234 D/Mono (10277): Assembly Ref addref Yourtime[0xc28778] -> System.Net.Http[0x1255c38]: 2 
04-28 02:15:20.244 D/Mono (10277): Assembly Ref addref System.Net.Http[0x1255c38] -> mscorlib[0xc76d38]: 14 
Resolved pending breakpoint at 'CameraFragment.cs:320,1' to void Yourtime.Fragments.CameraFragment.OnActivityResult (int requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00000]. 
Loaded assembly: System.Net.Http.dll 
04-28 02:15:20.505 D/Mono (10277): Image addref System.Xml[0x12c3d68] -> System.Xml.dll[0x12c9890]: 1 
04-28 02:15:20.505 D/Mono (10277): Assembly System.Xml[0x12c3d68] added to domain RootDomain, ref_count=1 
04-28 02:15:20.505 D/Mono (10277): AOT module 'System.Xml.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Xml.dll.so' not found 
04-28 02:15:20.505 D/Mono (10277): AOT module '/Users/builder/data/lanes/3053/a94a03b5/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Xml.dll.so' not found: Cannot load library: load_library[1091]: Library '/data/data/com.dualdub.androclient/lib/libaot-System.Xml.dll.so' not found 
04-28 02:15:20.535 D/Mono (10277): Unloading image data-0x63800008 [0x12c4080]. 
04-28 02:15:20.535 D/Mono (10277): Assembly Ref addref Mono.Android[0xca8958] -> System.Xml[0x12c3d68]: 2 
04-28 02:15:20.545 D/Mono (10277): Assembly Ref addref System.Xml[0x12c3d68] -> mscorlib[0xc76d38]: 15 
Loaded assembly: System.Xml.dll 
04-28 02:15:21.285 I/dalvikvm(10277): Could not find method android.view.SurfaceView.<init>, referenced from method md5b48e0788c01ac4725f8b67ece4dbb3b8.CameraS.<init> 
04-28 02:15:21.285 W/dalvikvm(10277): VFY: unable to resolve direct method 18819: Landroid/view/SurfaceView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;II)V 
04-28 02:15:21.285 D/dalvikvm(10277): VFY: replacing opcode 0x70 at 0x0000 
04-28 02:15:21.386 D/libEGL (10277): loaded /system/lib/egl/libGLES_android.so 
04-28 02:15:21.386 D/libEGL (10277): loaded /system/lib/egl/libEGL_adreno200.so 
04-28 02:15:21.396 D/libEGL (10277): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
04-28 02:15:21.396 D/libEGL (10277): loaded /system/lib/egl/libGLESv2_adreno200.so 
04-28 02:15:21.456 I/Adreno200-EGLSUB(10277): <ConfigWindowMatch:2078>: Format RGBA_8888. 
04-28 02:15:21.466 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x63d17000 size:3768320 offset:0 fd:70 
04-28 02:15:21.476 E/  (10277): Can't open file for reading 
04-28 02:15:21.476 E/  (10277): Can't open file for reading 
04-28 02:15:21.476 D/OpenGLRenderer(10277): Enabling debug mode 0 
04-28 02:15:21.696 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x6430d000 size:7536640 offset:3768320 fd:73 
04-28 02:15:21.736 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x64a3d000 size:29986816 offset:26218496 fd:76 
04-28 02:15:25.840 D/TextLayoutCache(10277): Using debug level: 0 - Debug Enabled: 0 
04-28 02:15:25.890 I/Adreno200-EGLSUB(10277): <ConfigWindowMatch:2078>: Format RGBA_8888. 
04-28 02:15:25.890 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x67032000 size:9981952 offset:7999488 fd:83 
04-28 02:15:25.960 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x67b42000 size:43724800 offset:41742336 fd:89 
04-28 02:15:27.061 D/memalloc(10277): /dev/pmem: Mapped buffer base:0x6a4f5000 size:24375296 offset:22392832 fd:92 
04-28 02:15:29.023 D/OpenGLRenderer(10277): Flushing caches (mode 0) 
04-28 02:15:29.023 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x63d17000 size:3768320 offset:0 
04-28 02:15:29.023 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x6430d000 size:7536640 offset:3768320 
04-28 02:15:29.023 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x64a3d000 size:29986816 offset:26218496 
04-28 02:15:29.073 D/OpenGLRenderer(10277): Flushing caches (mode 0) 
04-28 02:15:29.083 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x67032000 size:9981952 offset:7999488 
04-28 02:15:29.083 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x67b42000 size:43724800 offset:41742336 
04-28 02:15:29.083 D/memalloc(10277): /dev/pmem: Unmapping buffer base:0x6a4f5000 size:24375296 offset:22392832 
04-28 02:15:29.554 D/OpenGLRenderer(10277): Flushing caches (mode 2 

我已經安裝在OnActionResultActivity的開始,但在調試前的應用程序崩潰的斷裂點得到的存在。有沒有人有任何想法如何從視頻選擇器獲取視頻數據?

+0

我無法用C#術語解釋這一點,但是您將'Uri'視爲包含文件系統上文件的路徑,這通常是不正確的。 – CommonsWare

+0

我在我的項目中的其他地方使用相同的代碼,它工作正常。調試器沒有達到這一點,注意到這一點,應用程序崩潰。 –

回答

2

呼叫startActivityForResult通過在取行動,你想要的視頻用戶從這樣的選擇:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Videos.Media.INTERNAL_CONTENT_URI), SELECT_VIDEO); 

然後重寫onActivityResult監聽用戶作出選擇。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
super.onActivityResult(requestCode, resultCode, data); if (requestCode == SELECT_VIDEO) 
if (resultCode == Activity.RESULT_OK) { 
    Uri selectedVIDEO = data.getData(); 
} 
} 

希望這會有所幫助,如果是的話,那麼請接受我的回答。 :)

+0

很感謝。我會試試這個。儘管使用相同的代碼,我可以選擇一個圖像。 –

+0

這對我有用!非常感謝!!! :) –

相關問題