Android writing files to the external storage device

Similar documents
Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

Accelerating Information Technology Innovation

Adapter.

Android Tutorial: Part 3

package import import import import import import import public class extends public void super new this class extends public super public void new

Android Workshop: Model View Controller ( MVC):

Android HelloWorld - Example. Tushar B. Kute,

Mobila applikationer och trådlösa nät, HI1033, HT2012

INTRODUCTION TO ANDROID

GUI Widget. Lecture6

Android Programming Lecture 2 9/7/2011

Android File & Storage

UNDERSTANDING ACTIVITIES

Lab 1: Getting Started With Android Programming

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

Data storage and exchange in Android

SD Card with Eclipse/Emulator

Upcoming Assignments Quiz Friday? Lab 5 due today Alpha Version due Friday, February 26

1. Simple List. 1.1 Simple List using simple_list_item_1

Terms: MediaPlayer, VideoView, MediaController,

1. Implementation of Inheritance with objects, methods. 2. Implementing Interface in a simple java class. 3. To create java class with polymorphism

The Basis of Data. Steven R. Bagley

Android Data Binding: This is the DSL you're looking for

LifeStreet Media Android Publisher SDK Integration Guide

Practical 1.ListView example

Arrays of Buttons. Inside Android

CSCU9YH: Development with Android

Login with Amazon. Getting Started Guide for Android apps

MOBILE APPLICATIONS PROGRAMMING

Introductory Mobile App Development


Mobila applikationer och trådlösa nät, HI1033, HT2013

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

PVRTrace. Quick Start Guide for Unrooted Android Devices

SharedPreferences Internal Storage External Storage SQLite databases

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

1D/2D android secondary development

<uses-permission android:name="android.permission.internet"/>

AN4375 Application note

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

Android Development Tutorial

Adapting to Data. Before we get to the fun stuff... Initial setup

Android & iphone. Amir Eibagi. Localization

App Development for Smart Devices. Lec #18: Advanced Topics

Programming Concepts and Skills. Creating an Android Project

COMP4521 EMBEDDED SYSTEMS SOFTWARE

E-Blocks wifi controlled by an Android device

LECTURE NOTES OF APPLICATION ACTIVITIES

Real-Time Embedded Systems

Writing Efficient Drive Apps for Android. Claudio Cherubino / Alain Vongsouvanh Google Drive Developer Relations

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Software Practice 3 Today s lecture Today s Task Porting Android App. in real device

South Africa Version Control.

Mobile User Interfaces

Android Application Development. By : Shibaji Debnath

Embedded Systems Programming - PA8001

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

Mobile Application Development

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Overview of Activities

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

API Guide for Gesture Recognition Engine. Version 2.0

ListView Containers. Resources. Creating a ListView

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Introduction To Android

Getting Started With Android Feature Flags

Intents. 18 December 2018 Lecture Dec 2018 SE 435: Development in the Android Environment 1

Android Application Development 101. Jason Chen Google I/O 2008

Debugging Arts of the Ninja Masters. Justin Mattson Developer Advocate Android Google 28/5/2009

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Using Libraries, Text-to-Speech, Camera. Lecture 12

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

Android UI Development

Data Structures. 03 Streams & File I/O

Overview. Lecture: Implicit Calling via Share Implicit Receiving via Share Launch Telephone Launch Settings Homework

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

Orientation & Localization

Mobile Computing Practice # 2c Android Applications - Interface

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

Android Beginners Workshop

Input-Output and Exception Handling

API Guide for Gesture Recognition Engine. Version 1.1

COMP4521 EMBEDDED SYSTEMS SOFTWARE

IT101. File Input and Output

Workshop. 1. Create a simple Intent (Page 1 2) Launch a Camera for Photo Taking

EMBEDDED SYSTEMS PROGRAMMING UI and Android

CS 234/334 Lab 1: Android Jump Start

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial

Quizz Master AISL year wednesday 26 october 2016

LECTURE 12 BROADCAST RECEIVERS

Android Software Development Kit (Part I)

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Software Engineering Large Practical: Accessing remote data and XML parsing. Stephen Gilmore School of Informatics October 8, 2017

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Transcription:

Android writing files to the external storage device The external storage area is what Android knows as the SD card. There is a virtual SD card within the Android file system although this may be of size 0. This is not the same location as the SD card that may be added through a reader slot. If an application writes to the SD card other applications and the user can potentially access files at that location. To write to the SD location permission must be set in the AndroidManifest.xml file. This xml code has been added towards the end of the file: </application> <uses-permission android:name="android.permission.write_external_storage" /> </manifest> The files can be browsed within the AVD from the File Explore view Here the directory Download within mnt/sdcard has been created using the + button on the view. The file car.png will be transferred over by the Android program. Note that the directory is named Download but the code refers to it as.directory_downloads. The following code is from the developer.android.com site. The necessary imports have been fixed and the file car.png placed in res/drawable-hdpi. There are no controls on the GUI. The code will run as the activity loads. For the code to run correctly there must be a file named car.png within one of the res/drawable folders of the internal SD card.

public class ExternalStoreActivity extends Activity { /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //storage permission also set in AndroidManifest.xml if(environment.getexternalstoragestate().equals(environme nt.media_mounted)) Log.i("ExternalStorage", "Storage up"); //if this runs storage is up else Log.i("ExternalStorage", "Storage down"); createexternalstoragepublicpicture(); // end of oncreate void createexternalstoragepublicpicture() { // Create a path where we will place our picture in the user's // public pictures directory. File path = Environment.getExternalStoragePublicDirectory( File file = new File(path, "car.png"); try { // Make sure the Pictures directory exists. path.mkdirs(); //. Note that this code does no error checking, Log.i("ExternalStorage", "about to create is"); InputStream is = getresources().openrawresource(r.drawable.car); OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); Log.i("ExternalStorage", "file os passed"); // Tell the media scanner about the new file so //that it is immediately available to the user. MediaScannerConnection.scanFile(this, new String[] { file.tostring(), null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onscancompleted(string path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); ); catch (IOException e) { // Unable to create file, likely because external //storage is not currently mounted. Log.w("ExternalStorage", "Error writing " + file, e);

The Log files write output to LogCat a useful view that shows how far the file writing has got and give some idea of where it might have gone wrong. Here the 7374 output is of interest. If something is going wrong the output will probably be in orange or red. To get the file back or load any file from the SD card we need the absolute path of the file. There is no error handling here and the file name is hard coded but the path is picked up and the image loaded. String getexternalstoragepublicpicture() { File path = Environment.getExternalStoragePublicDirectory( File file = new File(path, "car.png"); String fullpath = file.getabsolutepath(); return fullpath; The above code will only be of any use if the file car.png exists in the Download directory of the external SD card. The method is called and the String containing the absolute file path assigned to an ImageView (ivdisplay). String filename = getexternalstoragepublicpicture() ; Log.i("filename is ", filename); Bitmap mybitmap = BitmapFactory.decodeFile(fileName); ivdisplay.setimagebitmap(mybitmap);

File browsing. There is no built-in file browsing widget but we can find all the files in a stated path and put them in an array. void findfilesinpath() { File path = Environment.getExternalStoragePublicDirectory( String spath = path.getabsolutepath(); File f = new File(sPath); File[] files = f.listfiles(); for(int x=0;x<files.length;x++) Log.i("Path files", files[x].getabsolutepath()); The log output shows that 2 files (only 2 were present) have been found and their paths identified. 03-19 19:22:38.204: INFO/Path files(5020): /mnt/sdcard/download/hang0.png 03-19 19:22:38.204: INFO/Path files(5020): /mnt/sdcard/download/car.png A straightforward, if ugly, approach to picking files is to map the array into the rows of a Spinner object. The user can then select an item from the dropdown on the spinner. The following code is based on that used in Linking Andriod and XML. The data from the spinner (splist) is output to a Toast for checking. This section is added at the end of findfilesinpath() String []filenames = new String[files.length]; for(int x=0;x<files.length;x++) filenames[x]=files[x].getabsolutepath(); if(files.length >0) {//MyOnItemSelectedListener() is declared later splist.setonitemselectedlistener(new MyOnItemSelectedListener()); ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.r.layout.simple_spinner_item, filenames); aa.setdropdownviewresource( android.r.layout.simple_spinner_dropdown_item); splist.setadapter(aa); //method ends

This is the code for the inner class: public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onitemselected(adapterview<?> parent,view view, int pos, long id) { Toast.makeText(parent.getContext(), "The file is " + parent.getitematposition(pos).tostring(), Toast.LENGTH_LONG).show(); public void onnothingselected(adapterview parent) { // Do nothing We need to take the name of the file and place it in the path that is used by the internal SD card. This can be achieved by overloading the existing method String getexternalstoragepublicpicture(string filename) { // new parameter added for the filename File path = Environment.getExternalStoragePublicDirectory( File file = new File(path, filename); //filename not car.png String fullpath ; fullpath = file.getabsolutepath(); return fullpath; As it is the onitemselected method of MyOnItemSelectedListener that chooses an item from the list. The same method can be used to move that image to the ImageView. The load from SD Button is now redundant.

public void onitemselected(adapterview<?> parent,view view, int pos, long id) { String filename = getexternalstoragepublicpicture(parent.getitematposi tion(pos).tostring()) ; Bitmap mybitmap = BitmapFactory.decodeFile(fileName); ivdisplay.setimagebitmap(mybitmap);