Android 4 returns null to download folder - android

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);

Related

Sharing audio file with WhatsApp fails on Android 4.2

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)

Osmand build crash on debug

I imported the latest master folder (master branch 26-12-2014) to Android-Studio,Gradle build the project successfully after little fix (asset missing,inner fragment static error) then i launched the debugger on my device (samsung galaxy 3 min,Android 4.1.2),I expect to have Osmand running on my device but the application crashes and the log indicate that it happen on getting routing.xml file.
12-26 10:47:10.069 10508-10508/net.osmand.plus E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create application net.osmand.plus.OsmandApplication: java.lang.IllegalStateException: java.lang.IllegalArgumentException
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4200)
at android.app.ActivityThread.access$1400(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: java.lang.IllegalArgumentException
at net.osmand.router.RoutingConfiguration.getDefault(RoutingConfiguration.java:171)
at net.osmand.plus.helpers.AvoidSpecificRoads.getBuilder(AvoidSpecificRoads.java:44)
at net.osmand.plus.helpers.AvoidSpecificRoads.<init>(AvoidSpecificRoads.java:38)
at net.osmand.plus.OsmandApplication.onCreate(OsmandApplication.java:168)
Thanks.
Make sure that there is a folder named "router" inside your "OsmAnd/scr/net/osmand/" folder which contains the routing.xml file.
If not you can find the resources needed here

Trying to run Android game using libgdx on Genymotion

Anybody experienced with libgdx and genymotion?
I'm trying to get a libgdx game running on genymotion android emulator - http://www.genymotion.com/
Everytime I try to run on genymotion I get the following exception:
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
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)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx' for target: Linux, 32-bit
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:113)
at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:34)
at com.badlogic.gdx.backends.android.AndroidApplication.<clinit>(AndroidApplication.java:61)
... 15 more
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load gdx: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:109)
... 17 more
Any Ideas?
Works fine with AVD
Android API 16
libgdx 0.9.9
Genymotion 1.1.0
You should try to add the x86 native libraries to your libs folder as mentioned in this post
To use the x86 natives, simply copy the x86/ folder from the nightlies to your Android project’s libs/ folder (next to the armeabi and armeabi-v7a folder). The setup-ui will not add those libraries for you automatically. I opted to do it this way for the time being as x86 Android devices are super rare, and you’d just blow up the size of your APK unnecessarily.

(Android) ANTLRStringStream throwing exception

I've been trying to get ANTLR-3.5-complete.jar to work on an Android app, but I can't get it to work! Bart Kiers's answer HERE has helped me a lot, but I still can't get it to work.
I'm trying to make a Button that, when clicked, will validate whether the text in the EditText view is correct according to my grammar.g, but it doesn't matter whether the text I input is correct or not, my app always crashes when I click the button while instantiating the ANTLRStringStream. Here's what I have:
public void onClickVerify(View v) throws TypeNotPresentException {
String source = "foobar";
Log.e("TestDebug", "Instantiating views");
TextView out = (TextView) findViewById(R.id.textView1);
EditText in = (EditText) findViewById(R.id.editText1);
try {
Log.e("TestDebug", "Getting source from EditText");
source = in.getText().toString();
Log.e("TestDebug", "source = " + source);
Log.e("TestDebug", "Instantiating stream");
ANTLRStringStream stream = new ANTLRStringStream(source);
Log.e("TestDebug", "Instantiating lexer");
grammarLexer lexer = new grammarLexer(stream);
Log.e("TestDebug", "Instantiating parser");
grammarParser parser = new grammarParser(
new BufferedTokenStream(lexer));
Log.e("TestDebug", "Applying rule to parser");
out.setText(source + " = " + !parser.failed());
} catch (IllegalStateException ise) {
out.setText("Exception caught");
}
}
And here's what I see in the logs:
Instantiating views
Getting source from EditText
source = test33
Instantiating stream
And then the "Unfortunately, TestAntlrApp has stopped." window pops up.
EDIT: Here's more of my logcat.
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
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: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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
... 11 more
Caused by: java.lang.NoClassDefFoundError: org.antlr.runtime.ANTLRStringStream
at com.test.antlr.MainActivity.onClickVerify(MainActivity.java:89)
Something about not finding the class ANTLRStringStream class? However there seems to be no errors/warnings in the code and I can even see the class's hierarchy.
Due to a recent ADT update, you can't just add external libraries to the Build Path using Eclipse's conventional way of doing it. Instead, you have to put it inside the libs folder for it to be packed within the .apk. If you simply add it to the build path, it will be ignored when building your .apk.
Symptom : The app was not recognizing external library classes.
Solution : Put the .jar file in the libs folder.

Android - Force close when no sd card is present

So I have an application which stores files in directories that I create when the user gets within a certain distance of a geographic location. Everything works fine except for when certain devices that usually use an sd card have it removed and then the app crashes when a user logs in. I tried on a droid x and I was able to replicate the crash. When I have the sd card in it works fine but when the sd card is removed I get a nullpointer exception and force close.
Heres the log dump from the market:
java.lang.RuntimeException: Unable to resume activity {graffit.cores/graffit.cores.GraffView}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2208)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1721)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at graffit.cores.GraffView.onResume(GraffView.java:1201)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3882)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2191)
... 12 more
java.lang.NullPointerException
at graffit.cores.GraffView.onResume(GraffView.java:1201)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3882)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2191)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1721)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Being that the logdump says something about failing on instrumentation Im guessing that I should catch that or look for if an sd card is available.
Im sure I can find out how to look for an sd card but how can I catch this crash and use internal storage in place of a missing sd card.
Any help would be much appreciated.
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might not always be available, such as in your case. The Android Docs give an example on how to deal with this: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
The best thing to do is probably do this check in a function that returns a Boolean value on whether you can get to the external storage and then check it each time you need to get data from the card.
if (sdCardAvalableForRead())
{
// get file.
}
else
{
// show warning or message to user.
}

Categories

Resources