Forum Xamarin.Forms

Media Plugin problem

AndriexAndriex USMember

Hi!
I have a problem when I am taking a photo with the media plugin on some android versions. It works on almost all versions of android, but on some versions (like 4.4.2 or 5.0.1) it says that I should put the write_external_storage permission in the manifest, but it is already there. On these versions, I also had problems writing files in Android.OS.Environment.ExternalStorageDirectory.Path so I wrote them in Environment.SpecialFolder.Personal. I tried to put a full path when using the media picker but it doesn't let me do that.
How can I take a photo in these versions?

Answers

  • Richy_GeorgeRichy_George INMember ✭✭✭

    Android

    In your BaseActivity or MainActivity (for Xamarin.Forms) add this code:
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    The WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE permissions are required, but the library will automatically add this for you. Additionally, if your users are running Marshmallow the Plugin will automatically prompt them for runtime permissions.
    Additionally, the following has been added for you:
    [assembly: UsesFeature("android.hardware.camera", Required = false)]
    [assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
    ANDROID N
    If your application targets Android N (API 24) or newer, you must use version 2.6.0+.
    You must also add a few additional configuration files to adhere to the new strict mode:
    1.) Add the following to your AndroidManifest.xml inside the tags:



    YOUR_APP_PACKAGE_NAME must be set to your app package name!
    2.) Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml
    Add the following code:
    <?xml version="1.0" encoding="utf-8"?>




    YOUR_APP_PACKAGE_NAME must be set to your app package name!

    Portable

    1. Ensure you call CrossMedia.Current.Initialize(); in App.cs
    2. This is a sample camera button click function
      async void OnTakepicClicked(object sender, EventArgs args)
      {
      // Camera Pick
      if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
      {
      // Supply media options for saving our photo after it's

              var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
              {
                  Directory = "YourApps",  ///    Give Your App Name
                  Name = $"profile.jpg"     ///     Give your pic name
              };
      
      
              // Take a photo of the business receipt.
              var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);
              if (file == null)
                  return;
      
              await DisplayAlert("File Location", file.Path, "OK");
      
      // Converting files tream to byte array (byte[] imageBytes)
              using (var memoryStream = new MemoryStream())
              {
                  file.GetStream().CopyTo(memoryStream);
                  imageBytes = memoryStream.ToArray();
                  flag = 1;
              }
      // Setting value to image source (profPic.Source  change to your image Source)
              profPic.Source = ImageSource.FromStream(() =>
              {
                  var stream = file.GetStream();
                  file.Dispose();
                  return stream;
              });
          }
      }
      
  • vishnutejavishnuteja USMember ✭✭

    Hi Richy , i have followed all your code, how u given above and i am getting following exception ,mainly unhandle exception

    {Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/4009/3a62f1ea/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00078] in <27c17fe440cf491ba8255bcefade6e02>:0
    at Plugin.Media.MediaPickerActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00212] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaPickerActivity.cs:172
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
    at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 at Plugin.Media.MediaImplementation+<TakePhotoAsync>d__14.MoveNext () [0x000d0] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:119 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
    at MyApps.BMC.Views.Sample+d__1.MoveNext () [0x000e9] in D:\XAMARIN.App\MyApps.BMCV2.0\MyApps.BMC\MyApps.BMC\MyApps.BMC\Views\Sample.xaml.cs:32
    --- End of managed Java.Lang.NullPointerException stack trace ---
    java.lang.NullPointerException
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.n_onCreate(Native Method)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
    at android.app.Activity.performCreate(Activity.java:5264)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2402)
    at android.app.ActivityThread.access$900(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1333)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5304)
    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:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
    }

  • vishnutejavishnuteja USMember ✭✭

    Hi Richy , i have followed all your code, how u given above and i am getting following exception ,mainly unhandle exception

    {Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/4009/3a62f1ea/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00078] in <27c17fe440cf491ba8255bcefade6e02>:0
    at Plugin.Media.MediaPickerActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00212] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaPickerActivity.cs:172
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
    at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 at Plugin.Media.MediaImplementation+<TakePhotoAsync>d__14.MoveNext () [0x000d0] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:119 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
    at MyApps.BMC.Views.Sample+d__1.MoveNext () [0x000e9] in D:\XAMARIN.App\MyApps.BMCV2.0\MyApps.BMC\MyApps.BMC\MyApps.BMC\Views\Sample.xaml.cs:32
    --- End of managed Java.Lang.NullPointerException stack trace ---
    java.lang.NullPointerException
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.n_onCreate(Native Method)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
    at android.app.Activity.performCreate(Activity.java:5264)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2402)
    at android.app.ActivityThread.access$900(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1333)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5304)
    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:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
    }

  • vishnutejavishnuteja USMember ✭✭

    Hi Richy , i have followed all your code, how u given above and i am getting following exception ,mainly unhandle exception

    {Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/4009/3a62f1ea/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00078] in <27c17fe440cf491ba8255bcefade6e02>:0
    at Plugin.Media.MediaPickerActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x00212] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaPickerActivity.cs:172
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
    at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 at Plugin.Media.MediaImplementation+<TakePhotoAsync>d__14.MoveNext () [0x000d0] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:119 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
    at MyApps.BMC.Views.Sample+d__1.MoveNext () [0x000e9] in D:\XAMARIN.App\MyApps.BMCV2.0\MyApps.BMC\MyApps.BMC\MyApps.BMC\Views\Sample.xaml.cs:32
    --- End of managed Java.Lang.NullPointerException stack trace ---
    java.lang.NullPointerException
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.n_onCreate(Native Method)
    at md5a3e164e78ade0c22cefea770ddd0bc49.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
    at android.app.Activity.performCreate(Activity.java:5264)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2402)
    at android.app.ActivityThread.access$900(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1333)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5304)
    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:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
    }

Sign In or Register to comment.