Tuesday, August 24, 2010

Android Web Gallery Example

I’ve been dabbling since last Friday with Android development. So far, I’ve already released one app on the marketplace because its pretty straight-forward to get something done on the Android platform.

My next challenge was to implement a picture gallery app and I found the following example:

http://www.androidpeople.com/android-gallery-imageview-example/

Also found an example of how to fetch images from the Web:

http://www.androidpeople.com/android-load-image-from-url-example/

So, all I needed to do was merge the two together…

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

webGalleryExample.java
package za.co.mtn.webGalleryExample;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryExample extends Activity {
private String[] jamCamURLs = {
http://path.to.image1.jpg”,
http://path.to.image2.jpg”,
http://path.to.image3.jpg”,

};
Drawable[] drawablesFromUrl = new Drawable[3];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for (int i = 0; i < drawablesFromUrl.length; i++){
drawablesFromUrl[i] = LoadImageFromURL(jamCamURLs[i]);
}
final ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageDrawable(drawablesFromUrl[0]);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));

g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageDrawable(drawablesFromUrl[position]);
}
});
}
private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
/*public static boolean StoreByteImage(Context mContext, byte[] imageData,
int quality, String expName) {

File sdImageMainDirectory = new File("/sdcard/myImages");
FileOutputStream fileOutputStream = null;
String nameFile = null;
try {

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length,options);

fileOutputStream = new FileOutputStream(
sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);

myImage.compress(CompressFormat.JPEG, quality, bos);

bos.flush();
bos.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return true;
}*/

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.JamCam);
mGalleryItemBackground = a.getResourceId(
R.styleable.JamCam_android_galleryItemBackground, 0);
a.recycle();
}

public int getCount() {
return jamCamURLs.length;
//return mImageIds.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);

i.setImageDrawable(drawablesFromUrl[position]);
i.setLayoutParams(new Gallery.LayoutParams(70, 57));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

return i;
}
}
}

And voila!


Monday, August 23, 2010

The Mountain

[caption id="" align="alignnone" width="422" caption="Dude, its the mountain. Like fully. Wicked."]uhhh, like where's mountain dude? [/caption]

Table Mountain Live Webcam