Android Help - Teaching

knapsackyarnΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

110 εμφανίσεις

Android Help
Section 8
Eric Xiao
The Midterm


That happened


Any residual questions?
New Assignment!


Make a low-fi prototype


Must be interactive


Use
balsamiq
or paper


Test it with users


3 tasks


Test task to show users how to use it (not one of your
tasks)


Only 1 user may be a student in this class


Everyone takes notes, discuss notes


Modify it
Outline


Individual Programming Assignment 3


Location Sensing


Android Camera


API usage


Display Image from Internet
Programming Assignment 3


getAWAYcam



Take a picture


Get
geolocation
and time from the metadata


Retrieve picture from Flickr API that has the same
location


See picture from someone else’s perspective


View pictures


Can’t view any pictures taken with this app until you
leave the location you took it from.
Location Services Demo
Source code:


https://
github.com
/
krushton
/CS160-Location-
Sample

Location Services


Requires permissions:
<uses-­‐permission  
android:name
=  
"
android.permission.ACCESS_FINE_LOCATION
"  />  
<uses-­‐permission  
android:name
=  
"
android.permission.ACCESS_COARSE_LOCATION
"  />  
Location Services
High Level


Initialize Location Manager


Get best provider from location services


Cell tower,
Wifi
, etc.


Get last known location

onLocationChanged
(Location  location)  


Do something with new location


Runs every time location changes
Location Services
void  
checkLocation
(View  v)  {  
 
//initialize  location  manager
 
 
 
manager  =    (
LocationManager
)    
 
getSystemService
(
Context.
LOCATION_SERVICE
);  
 
 
//get  a  location  provider  from  location  manager  
 
//empty  criteria  searches  through  all  providers    
 
and  returns  the  best  one  
 
 
String  
providerName
 =  
manager.getBestProvider
(
new    
 
Criteria(),  true);  
 
Location  location  =    
 
manager.getLastKnownLocation
(
providerName
);  
}  
Location Services
@Override  
public  void  
onLocationChanged
(Location  location)  
{  
 
TextView
 
tv
 =  (
TextView
)  
 
findViewById
(
R.id.
locationResults
);  
 
if  (location  !=  null)  {  
   
tv.setText
(
location.getLatitude
()  +  "    
   
latitude,  "  +  
location.getLongitude
()  +    
   
“longitude");  
 
}  
else
 {  
   
tv.setText
("Problem  
getting
 location");  
 
}  
}
 
Location Services


The cached location returned by
getLastKnownLocation
may be inaccurate.
To obtain updated location information,
implement the
LocationListener
interface
public class
LocationActivity
extends Activity
implements
LocationListener
{




This interface has 4 methods to override:
onLocationChanged
(Location l) {}
onProviderDisabled
(String provider) {}
onProviderEnabled
(String provider) {}
onStatusChanged
(String provider,
int
status, Bundle extras) {}

Location in the Emulator


The Android emulator has configurable settings
used to simulate location sensing.


Start the emulator, then open your SDK folder
and under \
sdk
\tools run
monitor.bat



In Android Debug
Monitor click the
Emulator Control
tab.


Edit the textboxes,
then click
Send
to
update the location.
Camera in Android
High Level


Make an intent to launch existing camera


Also include property to save image after exiting


Run
onActivityResult
() with picture data and
save it
Camera in Android
//  create  Intent  to  take  a  picture  and  return  control  
to  the  calling  application  
Intent  intent  =  
new  
Intent(
MediaStore.
ACTION_IMAGE_CAPTURE
);  
Uri  
fileUri
 =  
getOutputMediaFileUri
(MEDIA_TYPE_IMAGE);    
//  create  a  file  to  save  the  image  
intent.putExtra
(
MediaStore.
EXTRA_OUTPUT
,  
fileUri
);    
//  set  the  image  file  name  
         
//  start  the  image  capture  Intent  
startActivityForResult
(intent,  
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
 
Camera in Android
protected  void  
onActivityResult
(
int
 
requestCode
,  
int
 
resultCode
,  
Intent  data)  {  
           
if  (
requestCode
 ==  
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)  {  
                   
if  (
resultCode
 ==  
RESULT_OK)  {  
                           //  Image  captured  and  saved  to  
fileUri
 specified    
         
in  the  Intent  
       
 File  file  =  new  File(
fileUri.getPath
());  
       
   
       
//if  EXTRA_OUTPUT  not  specified,  this  will  work  
       
 Bitmap  photo  =  
data.getExtras
().get(“data”);  
}  
else  if  (
resultCode
 ==  
RESULT_CANCELED)  {  
                           //  User  cancelled  the  image  capture  
                   }  
else
 {  
                           //  Image  
capture
 
failed
,  
advise
 
user
 
                   }  
           }  
}
 
Camera
Links for problems/errors:


http://stackoverflow.com/questions/14558240/
onactivityresult-returned-from-a-camera-intent-
null


http://stackoverflow.com/questions/12564112/
android-camera-onactivityresult-intent-is-null-if-it-
had-extras



http://stackoverflow.com/questions/6341329/
built-in-camera-using-the-extra-mediastore-
extra-output-stores-pictures-twice

Camera


Example Code:
http://www.compiletimeerror.com/2013/08/
android-camera-app-in-android-example.html

API Calls
HttpClient
 client  =  
new  
DefaultHttpClient
();  
HttpGet
 request  =  
new  
HttpGet
();  
 
request.setURI
(
new  URI("http://
www.panoramio.com
/map/
get_panoramas.php?
set
=
public&from
=0&to="+
randomInt
+"&minx="+
minLat
+"&
miny
="+
minLng
+"&
maxx
="+
maxLat
+"&
maxy
="+
maxLng
+"&size=
medium&mapfilter
=true"));  
 
response  =  
client.execute
(request);  
//Parse  string  to  get  photo  
Panoramio
API


http://www.panoramio.com/api/data/api.html



Flickr is also an option still too


Use any Photo API you’d like
Displaying Image from the Internet
ImageView
 iv  =  new  
ImageView
;  
URL  
url
 =  new  URL(address);  
InputStream
 content  =  
(
InputStream
)
url.getContent
();  
Drawable
 d  =  
Drawable.createFromStream
(content  ,  
"
src
");    
iv.setImageDrawable
(d)  
 
 
Display Image from Internet


http://stackoverflow.com/questions/4223472/
how-to-display-image-from-internet-in-android