简体   繁体   中英

Can't use Xam.Plugin.Media plugin

I have android application that is built using Xamarin Forms. In order to use camera of the mobile, I install Xam.Plugin.Media plugin and follow exactly the instructions of this video .

This is my code:

public async void TakePhotoAsAttachment(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No Camera", ":( No camera available.", "OK");
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg"
            });

            if (file == null)
                return;

            await DisplayAlert("File Location", file.Path, "OK");

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                return stream;
            });
        }

However, when executing this code:

        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Sample",
            Name = "test.jpg"
        });

I get this error:

{Java.Lang.ClassNotFoundException: md55b01af31820394fbfb172542d1258596.MediaPickerActivity ---> Java.Lang.ClassNotFoundException: Didn't find class "md55b01af31820394fbfb172542d1258596.MediaPickerActivity" on path: DexPathList[[zip file "/data/app/com.companyname.EClinicForDoctor-1/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.EClinicForDoctor-1/lib/arm, /data/app/com.companyname.EClinicForDoctor-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]] --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <657aa8fea4454dc898a9e5f379c58734>:0 at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <54816278eed9488eb28d3597fecd78f8>:0 at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in :0 at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00017] in :0 at Android.Runtime.JNIEnv.FindClass (System.String classname) [0x0003d] in :0 at Android.Runtime.JNIEnv.FindClass (System.Type type) [0x00084] in :0 at Java.Lang.Class.FromType (System.Type type) [0x00022] in :0
at Android.Content.Intent..ctor (Android.Content.Context packageContext, System.Type type) [0x00000] in :0 at Plugin.Media.MediaImplementation.CreateMediaIntent (System.Int32 id, System.String type, System.String action, Plugin.Media.Abstractions.StoreMediaOptions options, System.Boolean tasked) [0x00000] in C:\\projects\\mediaplugin\\src\\Media.Plugin.Android\\MediaImplementation.cs:411 at Plugin.Media.MediaImplementation.TakeMediaAsync (System.String type, System.String action, Plugin.Media.Abstractions.StoreMediaOptions options) [0x00044] in C:\\projects\\mediaplugin\\src\\Media.Plugin.Android\\MediaImplementation.cs:470 at Plugin.Media.MediaImplementation+d__17.MoveNext () [0x000ab] in C:\\projects\\mediaplugin\\src\\Media.Plugin.Android\\MediaImplementation.cs:153

--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <657aa8fea4454dc898a9e5f379c58734>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <657aa8fea4454dc898a9e5f379c58734>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <657aa8fea4454dc898a9e5f379c58734>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <657aa8fea4454dc898a9e5f379c58734>:0 at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <657aa8fea4454dc898a9e5f379c58734>:0 at EClinicForDoctor.Pages.VisitsPage+d__17.MoveNext () [0x00175] in E:\\Doctor Project\\Mobile Applications\\v1\\4\\EClinicForDoctor\\EClinicForDoctor\\EClinicForDoctor\\Pages\\VisitsPage.xaml.cs:235 --- End of managed Java.Lan g.ClassNotFoundException stack trace --- java.lang.ClassNotFoundException: md55b01af31820394fbfb172542d1258596.MediaPickerActivity at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:324) at md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.n_onClick(Native Method) at md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10826) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.ma in(ZygoteInit.java:1120) Caused by: java.lang.ClassNotFoundException: Didn't find class "md55b01af31820394fbfb172542d1258596.MediaPickerActivity" on path: DexPathList[[zip file "/data/app/com.companyname.EClinicForDoctor-1/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.EClinicForDoctor-1/lib/arm, /data/app/com.companyname.EClinicForDoctor-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) ... 14 more Suppressed: java.lang.ClassNotFoundException: md55b01af31820394fbfb172542d1258596.MediaPickerActivity at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 15 more Caused by: java.lang.NoClassDefFoundError: Clas s not found using the boot class loader; no stack trace available }

Then I read the instructions of the plugin from github which it was somehow similar to the instructions of the video and the same error occur. How can solve this problem?

Try to delete the bin obj folder of your project.

Your compile code and NuGet sits in obj, bin folder. Sometimes when you add any new NuGet it doesn't go in obj, bin folder. It only use earlier compiled NuGet as cache from bin and obj. Deleting all bin, obj force compiler to create new bin obj folder and recompile the solution. It is better idea to delete bin, obj whenever you add any new NuGet in solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM