Posts in the “android” category

Android AsyncTask (REST client): A source code 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.

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

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

Last night I was trying to use the Android File Transfer program on my MacOS system to transfer music to my Google Nexus 9, which now runs Android 6 Android 7. After I connected my Nexus 9 tablet to the Mac with its USB cable and then started the Android File Transfer app, I saw this error message on my Mac:

Can’t access device storage. If your device’s screen is locked, disconnect its USB cable, unlock your screen, and then reconnect the USB cable.

This is what the Mac error message dialog looks like:

Android File Transfer - Can't access device storage

Unfortunately that’s a misleading and unhelpful error message, as the problem has nothing to do with the Android device screen being locked.

The solution

The solution to the problem on Android 7, Android 6 (and maybe Android 5) is to unlock your Android device (if it isn’t unlocked already), pull down the list of notifications, then tap the “USB for charging” notification:

Tapping that notification brings up the following dialog:

Transfer files (MTP)

On this dialog you want to tap the “Transfer files (MTP)” option, as indicated by that large red arrow.

Note that this option has changed names at least once. As of March, 2017, it is now labeled “Transfer files,” with the subtitle, “Transfer files to another device.”

When you do this, the Android File Transfer app will either automatically start (which it does on my Mac), or you can go ahead and start it manually, at which point you’ll see the Android File Transfer main window:

Android File Transfer app main window (success)

Once you see this window you can go ahead and start dragging files from your Mac (or Windows) computer to this Android File Transfer window.

Note: Be sure to click OK on the Mac

As Gert notes in the comments below, before you tap MTP on the phone, make sure you click “OK” on your Mac so that error message goes away.

A quick note about transferring music files

Note that if you’re transferring music files to your Android device, you’ll want to transfer them to the Music folder. To do this, double-click that folder to open it, then drag and drop music files from your PC into that folder. On a Mac you do this by opening a Finder window, navigating to the folder where your music files are located — such as /Users/Al/Music/iTunes/iTunes Music — and then dragging and dropping the files from the Mac Finder window to the Android File Transfer window. Then start the “Play Music” app on your Android device and you should see your files under the “Recent activity” area on the home page of that app.

Summary

In summary, if you get the “Can’t access device storage” error when using the Android File Transfer app when trying to transfer files from your Mac or Windows system to your Android phone or tablet, I hope this solution is helpful.

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/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:

/data/data/com.alvinalexander.mynewapp/files

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 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.

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]