Posts in the “android” category

Android/Java: How to get your app’s root data directory

If you ever need to get the root data directory of your Android application (app) from within your Java code, I can confirm that this approach works:

File rootDataDir = getActivity().getFilesDir();

When I log that directory like this:

Log.i(TAG, rootDataDir.toString());

it prints this output for my application:


where com.alvinalexander.mynewapp is the package name for my new Android app.

Basic Android “Toast” syntax examples

Android Toast FAQ: How do I create a Toast message in Android? (Or, Can you share some Android Toast message syntax examples?)

Here's one example of the Android Toast syntax:

Toast.makeText(ProjectActivity.this, "Your message here" , Toast.LENGTH_SHORT).show();

and here's a second example, this time referring to the Android application context as the first method parameter:

Android File Transfer error: Can’t access device storage (solved)

Summary: This article shows a solution to the Android File Transfer app “not working on Mac” problem, where you get the error message, “Can’t access storage device.” (This solution probably also works on Microsoft Windows systems, though I don’t know for sure.)

How to show an HTML string in an Android TextView

Filed under “What I learned about Android today,” you can display an HTML string in an Android TextView. However, this approach has severe limitations, and you’ll probably want to display your HTML in a WebView instead.

To display an HTML string in a TextView, you need to use the Android Html.fromHtml() method, as shown in this code:

Android emulator not loading my app

I haven't used Android in a little while now, in particular with my new laptop, and the first time I tried running an Android app from inside Eclipse, the Android emulator wouldn't finish starting properly and run my app.

I remember I used to look under the "all apps" icon, and could sometimes find my app was actually loaded, but in this case, it wasn't loaded at all.

Source code for an Android AsyncTask (REST client) example

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

How to create a SQLite database

SQLite database FAQ: How do I create a SQLite database?

Creating a new database in SQLite is so easy, it's amazing. Once you have SQLite installed and your PATH set up properly, from your Unix or DOS shell, just issue a SQLite command like this:

The Android “adb shell list files permission denied” error

As a brief note, today I tried to list the files in my Android application, which was running on a physical Android device — a Nexus 9 — with this adb shell command:

adb shell com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser

When I did that, I got an Android/ADB “permission denied” error.

The short story is that a solution to this problem is to run the same command, but with the run-as argument, like this:

adb shell run-as com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser

More information

The Android docs describe the run-as option:

Run commands on a device as an app (specified using the package name). This lets you run commands in adb as if the app you specify is running the command (that is, you have the same device access that the app has), without requiring root access. This might be necessary when using adb on a non-rooted device or an emulator with a Play store image. The app must be debuggable.

Related commands

I don’t have time to add much more to this right now, but one thing I’ll note is that if you have to run an ADB command where the file path has spaces in it, this command worked:

> adb shell run-as com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser/app_webview/Web\\ Data

How to copy files to an Android emulator’s data directory with ‘adb push’

As an Android developer, you can normally use the adb push command to copy files from your computer’s hard drive to an Android device. However, I just ran into a problem where I couldn’t copy files to my Android emulator’s “data” directory, i.e., the /data/data filesystem. When I tried to copy a file using this command:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files

I got this Android error:

SQLite: Default a datetime field to the current time (now)

SQLite FAQ: How do I default a SQLite datetime field to the current date and time? (i.e., how do I default it to now?)

Just use the SQLite current_timestamp function, like this:

last_updated datetime default current_timestamp

In a more complete create table example I just used on an Android project, this looks like this:

SQLite alter table syntax examples

SQLite FAQ: Can you show me how the SQLite ALTER TABLE syntax works?

At the time of this writing you can use the SQLite ALTER TABLE syntax for two purposes:

  1. Add a column to the end of an existing SQLite database table
  2. Change the name of a database table.

For other changes you'll have to follow some workaround procedures (discussed below).

[toc hidden:1]