• No results found

Accessing SD Cards

Some Android devices will include a slot for additional flash memory to be plugged in, typically a Secure Digital (SD) card. These memory cards, if present, are much larger than the built-in memory, and thus they’re ideal for storing multimegabyte music and video files. They cannot be used for code, and every application can read and write files there.

In Section5.2, Playing Video, on page112, we uploaded a sample video file to the /data directory of the emulated device. This is the wrong place for it, since we’re not supposed to put large files on the internal file system. So, now I’m going to show you a better way.

The first step is to create and format a virtual SD card that we can

“plug in” to the emulator. Luckily we’ve already done this—if you recall, in Section1.3, Creating an AVD, on page23when we created the “em22”

virtual device, we gave it a 64MB virtual SD card as well. You can make it any size you like, but if you make it too small, it may cause the emulator to crash; if you make it too big, you’ll just waste space on your computer’s disk drive.

Next, let’s copy the sample video to the SD card:

C:\> adb push c:\code\samplevideo.3gp /sdcard/samplevideo.3gp 1468 KB/s (369870 bytes in 0.246s)

Then we need to modify theonCreate( ) method of theVideoclass to play the movie from the SD card instead of the/datadirectory:

Download Videov2/src/org/example/video/Video.java

// Load and start the movie

video.setVideoPath("/sdcard/samplevideo.3gp");

video.start();

Now try to run the program. The video should play normally.

Note: Starting with Android 1.6, you will need to request the WRITE_

EXTERNAL_STORAGEpermission in your manifest file if you want to write to the SD card from your application. Reading from the card doesn’t require any special permissions.

Starting with Android 2.2, your application can use the Context.getEx-ternalFilesDir( ) method to get the directory on the external file system where it can place persistent files it owns. Android will delete the files when the application is uninstalled.

6.6 Fast-Forward >>

In this chapter, we covered a couple of basic ways to store local data on the Android platform. That should be enough to get you started, but for structured data such as phone lists and recipes, you’ll need some-thing more advanced. See Chapter9, Putting SQL to Work, on page178 for directions on how to use Android’s built-in SQLite database and how to share information between applications using content providers.

For instructions on installing applications on external storage, see Sec-tion13.6, Installing on the SD Card, on page268

This brings us to the end of Part II. With the help of the Sudoku exam-ple, you’ve learned all the basics of Android programming, including user interfaces, 2D graphics, audio, video, and simple data storage.

Now it’s time to leave Sudoku behind and move beyond the basics.

Part III

Beyond the Basics

The Connected World

Over the next few chapters, we’ll cover more advanced topics such as network access and location-based services. You can write many useful applications without these features, but going beyond the basic fea-tures of Android will really help you add value to your programs, giving them much more functionality with a minimum of effort.

What do you use your mobile phone for? Aside from making calls, more and more people are using their phones as mobile Internet devices.

Analysts predict that in a few years mobile phones will surpass desktop computers as the number-one way to connect to the Internet.1 This point has already been reached in some parts of the world.2

Android phones are well equipped for the new connected world of the mobile Internet. First, Android provides a full-featured web browser based on the WebKit open source project.3 This is the same engine you will find in Google Chrome, the Apple iPhone, and the Safari desktop browser but with a twist. Android lets you use the browser as a compo-nent right inside your application.

Second, Android gives your programs access to standard network ser-vices like TCP/IP sockets. This lets you consume web serser-vices from Google, Yahoo, Amazon, and many other sources on the Internet.

1. http://archive.mobilecomputingnews.com/2010/0205.html 2. http://www.comscore.com/press/release.asp?press=1742 3. http://webkit.org

BROWSING BYINTENT 131

Figure 7.1: Opening a browser using an Android intent

In this chapter, you’ll learn how to take advantage of all these features and more through four example programs:

• BrowserIntent: Demonstrates opening an external web browser using an Android intent

• BrowserView: Shows you how to embed a browser directly into your application

• LocalBrowser: Explains how JavaScript in an embeddedWebView and Java code in your Android program can talk to each other

• Translate: Uses data binding, threading, and web services for an amusing purpose