Sharing audio file with WhatsApp fails on Android 4.2 - android

I am failing to share an audio file with WhatsApp on an Android Jelly Bean emulator. My code currently works on Lollipop and higher versions but I couldn't figure out why this error occurs on lower devices.
val share = Intent(Intent.ACTION_SEND)
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
//Audio MIME type
share.type = "audio/*"
//File uri (from file provider on api level 24 or higher)
share.putExtra(Intent.EXTRA_STREAM, fileUri(context))
context.startActivity(Intent.createChooser(share, context.getString(R.string.share_title)))
inside fileUri method
if (Build.VERSION.SDK_INT < 24)
return Uri.fromFile(this)
return FileProvider.getUriForFile(context, context.applicationContext.packageName
+ ".[myprovidername].provider", this)
logs
04-21 19:08:25.653 2840-2840/com.whatsapp W/Bundle: Key android.intent.extra.STREAM expected ArrayList but value was a android.net.Uri$HierarchicalUri. The default value <null> was returned.
04-21 19:08:25.653 2840-2840/com.whatsapp W/Bundle: Attempt to cast generated internal exception:
java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.util.ArrayList
at android.os.Bundle.getParcelableArrayList(Bundle.java:1223)
at com.whatsapp.ContactPickerFragment.ga(:250573)
at com.whatsapp.ContactPickerFragment.a(:249616)
at c.j.a.u.a(:178380)
at c.j.a.u.f(:178956)
at c.j.a.u.a(:177973)
at c.j.a.u.b(:178772)
at c.j.a.u.j(:179098)
at c.j.a.j.onStart(:246644)
at c.a.a.m.onStart(:267332)
at d.f.qJ.onStart(:296953)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

That's probably because WhatsApp is requiring a list of Uris even if there's only one. Try returning something like this
ArrayList<Uri> uriList = new ArrayList<Uri>();
uriList.add(Uri.fromFile(this));
Instead of
Uri.fromFile(this)

Related

What is the cause of an IllegalArgumentException at onRestoreInstanceState

There was multiple (more than 10) crashes reported in my Google Play Console with this stack trace (full):
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method:0)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method:0)
Caused by: java.lang.IllegalArgumentException:
at android.view.View.onRestoreInstanceState(View.java:12308)
at android.view.View.dispatchRestoreInstanceState(View.java:12284)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2617)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2623)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2623)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2623)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2623)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2623)
at android.view.View.restoreHierarchyState(View.java:12262)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1647)
at android.app.Activity.onRestoreInstanceState(Activity.java:938)
at android.app.Activity.performRestoreInstanceState(Activity.java:910)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)
What would trigger this exception? There was no mention of my application's classes in the stack trace. What should I do?
The following text is written in the Android source code in View.java:12308 as the IllegalArgumentException message:
Wrong state class, expecting View State but received (something else) instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is (view id). Make sure other views do not use the same id.

Android 4 returns null to download folder

i'm using the DownloadManager to download files and save in a folder inside downloads folder in my application context folder on internal storage, this works fine in Android 6 but in android 4 the path returns null.
This code works in android 6 but not in android 4
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
i have change to this and works
File p = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if(p == null){
p = getApplicationContext().getDir(Environment.DIRECTORY_DOWNLOADS, MODE_PRIVATE);
}
My problem is in DownloadManager, when i use the follow code an NullPointerReference occurs
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);
can anyone help me?
EDIT
This is the stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:441)
at android.app.DownloadManager$Request.setDestinationFromBase(DownloadManager.java:508)
at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:470)
at br.com.digitaldoc.ddv4.activities.MainActivity$7.onClick(MainActivity.java:405)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:958)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
at android.widget.AbsListView$1.run(AbsListView.java:3535)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4931)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
at dalvik.system.NativeStart.main(Native Method)
and this line MainActivity.java:405 is
req.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS + "/viewer", fileName);

SecurityException when calling BluetoothAdapter.getDefaultAdapter

In onCreate() of my app I call BluetoothAdapter.getAddress(). One single device of hundreds, that are running this app, yields a java.lang.SecurityException:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{xx.yyy.myapp/xx.yyy.myapp.RecActivity}:
java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither
user 10095 nor current process has android.permission.BLUETOOTH_ADMIN.
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:5039) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at
dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither
user 10095 nor current process has android.permission.BLUETOOTH_ADMIN.
at android.os.Parcel.readException(Parcel.java:1425) at
android.os.Parcel.readException(Parcel.java:1379) at
android.bluetooth.IBluetoothManager$Stub$Proxy.getAddress(IBluetoothManager.java:295)
at
android.bluetooth.BluetoothAdapter.getAddress(BluetoothAdapter.java:576)
at xx.yyy.myapp.RecActivity.onCreate(Unknown Source) at
android.app.Activity.performCreate(Activity.java:5104) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
As I can't debug on that device I would like to ask if the following solution is feasible or if there's a better way to handle the problem (this is the branch for less than JELLY_BEAN_MR2)?
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
try {
macAddress = ( bta != null ) ? bta.getAddress() : "";
} catch ( Exception e ) {
macAddress = "";
}
Also, I wonder if the Android version on that device might have a bug as it seems to me that getAddress() in BluetoothAdapter.java doesn't require android.permission.BLUETOOTH_ADMIN?
Or is it possible that the user with this device has a specific root tool to lock bluetooth access from my app? And that this might be the reason for the Exception?
Or what might be the reason for the problem?
Since it's bad practice to use catch(Exception e), use
} catch(SecurityException e){
to prevent the crash.
(As an answer now instead of a comment.)

Android Live Wallpaper crashes on Amazon App Store Testing ActivityNotFoundException

I am trying to put my live wallpaper on Amazon App Store(This live wallpaper is already live on Google Play Store), However, when I uploaded same APK for testing on Amazon Developer site it reports following exception
03-06 06:00:50.741 7598 7598 E AndroidRuntime: FATAL EXCEPTION: main
03-06 06:00:50.741 7598 7598 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rrapps.heavensdoor/com.rrapps.heavensdoor.PreviewActivity}: android.content.ActivityNotFoundException: No Activity found to handle act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:151)
at android.app.ActivityThread.main(ActivityThread.java:5204)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle act=android.service.wallpaper.CHANGE_LIVE_WALLPAPER (has extras)
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1627)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at com.rrapps.heavensdoor.PreviewActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5170)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
following is code where I am setting wallpaper
if (Build.VERSION.SDK_INT >= 16) {
/*
* Open live wallpaper preview (API Level 16 or greater).
*/
intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = MyWallpaperService.class.getPackage().getName();
String cls = MyWallpaperService.class.getCanonicalName();
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(pkg, cls));
} else {
/*
* Open live wallpaper picker (API Level 15 or lower).
*
* Display a quick little message (toast) with instructions.
*/
intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
Resources res = getResources();
String hint = res.getString(R.string.picker_toast_prefix)
+ res.getString(R.string.lwp_name)
+ res.getString(R.string.picker_toast_suffix);
Toast toast = Toast.makeText(this, hint, Toast.LENGTH_LONG);
toast.show();
}
startActivityForResult(intent, 0);
I am not sure what the problem is. Any help is appreciated.
Android is a funny world and implicit intents unfortunately, vary more than we (developers) would like them to. Specially for Amazon devices, that are not required to comply with Google test suite. On my app, I've seen ActivityNotFoundException trying to launch the web-browser.
So my suggestion, even tho it doesn't actually fix the issue, it's just try catch it:
try{
startActivityForResult(intent, 0);
} catch(ActivityNotFoundException e) {
// why amazon, why?
}

When I send HashMap with size more than 20 in android 4.1, I get crash

When I send HashMap with size more than 20 in android 4.1, I get crash with this log:
10-02 13:13:05.068: ERROR/AndroidRuntime(938): FATAL EXCEPTION: main
java.lang.SecurityException: Unable to find app for caller android.app.ApplicationThreadProxy#41bbb490 (pid=938) when publishing content providers
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.publishContentProviders(ActivityManagerNative.java:2392)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4199)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
What I do for that:
Intent intent = new Intent(MyActions.PREVIEWS);
HashMap<String, Bitmap> thumbnails = new HashMap<String, Bitmap>();
// added more than 20 items
intent.putExtra("previews", thumbnails);
setResult(RESULT_OK, intent);
finish();
You should save Bitmaps into files and pass their references to another activity. Extra's bundle size has limitations : https://groups.google.com/forum/?fromgroups=#!topic/android-developers/kwmTGz8GAoQ
Answer on this question also should help
You're probably exceeding the amount of data you can send in an Intent

Categories

Resources