Convert file: Uri to File in Android

What you want is… new File(uri.getPath()); … and not… new File(uri.toString()); Notes For an android.net.Uri object which is named uri and created exactly as in the question, uri.toString() returns a String in the format “file:///mnt/sdcard/myPicture.jpg”, whereas uri.getPath() returns a String in the format “/mnt/sdcard/myPicture.jpg”. I understand that there are nuances to file storage in Android. … Read more

Room – Schema export directory is not provided to the annotation processor so we cannot export the schema

In the build.gradle file for your app module, add this to the defaultConfig section (under the android section). This will write out the schema to a schemas subfolder of your project folder. javaCompileOptions { annotationProcessorOptions { arguments += [“room.schemaLocation”: “$projectDir/schemas”.toString()] } } Like this: // … android { // … (compileSdkVersion, buildToolsVersion, etc) defaultConfig { … Read more

Percentage width in a RelativeLayout

You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout. In the following example, the left button uses 70% of the space, and the right button 30%. <LinearLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”horizontal”> <Button android:text=”left” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.70″ /> <Button android:text=”right” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.30″ /> </LinearLayout> It works the … Read more

ADB Android Device Unauthorized

It’s likely that the device is no longer authorized on ADB for whatever reason. 1. Check if authorized: <ANDROID_SDK_HOME>\platform-tools>adb devices List of devices attached 4df798d76f98cf6d unauthorized 2. Revoke USB Debugging on phone If the device is shown as unauthorized, go to the developer options on the phone and click “Revoke USB debugging authorization” (tested with … Read more

What’s the difference between commit() and apply() in SharedPreferences

apply() was added in 2.3, it commits without returning a boolean indicating success or failure. commit() returns true if the save works, false otherwise. apply() was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous. http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

Android: How to handle right to left swipe gestures

OnSwipeTouchListener.java: import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeTouchListener (Context ctx){ gestureDetector = new GestureDetector(ctx, new GestureListener()); } @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends SimpleOnGestureListener { private static final … Read more

Android basics: running code in the UI thread

None of those are precisely the same, though they will all have the same net effect. The difference between the first and the second is that if you happen to be on the main application thread when executing the code, the first one (runOnUiThread()) will execute the Runnable immediately. The second one (post()) always puts … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)