Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 11    Views: 153

Suvinay pawa
buzztouch Evangelist
Profile
Posts: 599
Reg: Aug 01, 2012
location unknow...
9,890
01/20/13 02:14 AM (13 years ago)

Found code for image gallery..Works like a charm

Hi i found this gallery which sources image from url. looks awesome. Consider that your server contains two type of images, one is original and second is thumbnail. e.g. Thumbnail: th_3BC52DAA-AFD8-50B5-DA5096B82422275F.jpg Original: 3BC52DAA-AFD8-50B5-DA5096B82422275F.jpg main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" 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/GalleryView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> MyGallery.java package com.isummation.igallery; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; public class MyGallery extends Activity { private ImageAdapter imageAdapter; private ArrayList PhotoURLS = new ArrayList(); private ProgressDialog dialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageAdapter = new ImageAdapter(this); final ImageView imgView = (ImageView) findViewById(R.id.GalleryView); Gallery g = (Gallery) findViewById(R.id.Gallery); g.setAdapter(imageAdapter); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { dialog = ProgressDialog.show(ImageGallery.this, "Loading", "Please wait...", true); new ShowImageTask().execute(position); } }); // replace this code to set your image urls in list PhotoURLS.add("http://10.0.2.2/upload/3BC52DAA-AFD8-50B5-DA5096B82422275F.jpg"); PhotoURLS.add("http://10.0.2.2/upload/B652F78D-C760-B674-7A6E61E505A05A0F.jpg"); new AddImageTask().execute(); } class ShowImageTask extends AsyncTask { @Override protected Drawable doInBackground(Integer... position) { Drawable drawable = LoadImageFromURL(PhotoURLS.get(position[0])); return drawable; } @Override protected void onProgressUpdate(Void... unsued) { } @Override protected void onPostExecute(Drawable drawable) { imgView.setImageDrawable(drawable); imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); dialog.dismiss(); } } class AddImageTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... unused) { for (String url : PhotoURLS) { String filename = url.substring(url.lastIndexOf("/") + 1, url.length()); filename = "th_" + filename; String thumburl = url.substring(0, url.lastIndexOf("/") + 1); imageAdapter.addItem(LoadThumbnailFromURL(thumburl + filename)); publishProgress(); //SystemClock.sleep(200); } return (null); } @Override protected void onProgressUpdate(Void... unused) { imageAdapter.notifyDataSetChanged(); } @Override protected void onPostExecute(Void unused) { } } private Drawable LoadThumbnailFromURL(String url) { try { URLConnection connection = new URL(url).openConnection(); String contentType = connection.getHeaderField("Content-Type"); boolean isImage = contentType.startsWith("image/"); if(isImage){ HttpGet httpRequest = new HttpGet(url); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient .execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity); InputStream is = bufferedHttpEntity.getContent(); Drawable d = Drawable.createFromStream(is, "src Name"); return d; } else { Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.no_image); Drawable d = new BitmapDrawable(b); return d; } } catch (Exception e) { Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG) .show(); Log.e(e.getClass().getName(), e.getMessage(), e); return null; } } private Drawable LoadImageFromURL(String url) { try { URLConnection connection = new URL(url).openConnection(); String contentType = connection.getHeaderField("Content-Type"); boolean isImage = contentType.startsWith("image/"); if(isImage){ HttpGet httpRequest = new HttpGet(url); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient .execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity( entity); InputStream is = bufferedHttpEntity.getContent(); // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, o); // The new size we want to scale to final int REQUIRED_SIZE = 150; // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 &lt; REQUIRED_SIZE || height_tmp / 2 &lt; REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // Decode with inSampleSize is = bufferedHttpEntity.getContent(); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; Bitmap b = BitmapFactory.decodeStream(is, null, o2); Drawable d = new BitmapDrawable(b); return d; } else { Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.no_image); Drawable d = new BitmapDrawable(b); return d; } } catch (Exception e) { Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG) .show(); Log.e(e.getClass().getName(), e.getMessage(), e); return null; } } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; ArrayList drawablesFromUrl = new ArrayList(); public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(R.styleable.GalleryTheme); mGalleryItemBackground = a.getResourceId( R.styleable.GalleryTheme_android_galleryItemBackground, 0); a.recycle(); } public void addItem(Drawable item) { drawablesFromUrl.add(item); } public int getCount() { return drawablesFromUrl.size(); } public Drawable getItem(int position) { return drawablesFromUrl.get(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.get(position)); i.setLayoutParams(new Gallery.LayoutParams(80, 70)); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setBackgroundResource(mGalleryItemBackground); return i; } } } attrs.xml should be in values <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="GalleryTheme"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
 
acschcs
Lost but trying
Profile
Posts: 118
Reg: Jan 16, 2013
United Kingdom
2,730
like
01/20/13 02:16 AM (13 years ago)
For android only? And because I'm a newbie to costing and all that ... Where do I input this code?
 
Suvinay pawa
buzztouch Evangelist
Profile
Posts: 599
Reg: Aug 01, 2012
location unknow...
9,890
like
01/20/13 02:21 AM (13 years ago)
Ok you have to create a new java and two xml files and point some button tothis activity..or there is a easier way that is by using plugin creater tool
 
acschcs
Lost but trying
Profile
Posts: 118
Reg: Jan 16, 2013
United Kingdom
2,730
like
01/20/13 02:26 AM (13 years ago)
What... It's so complicated any tutorials?
 
Suvinay pawa
buzztouch Evangelist
Profile
Posts: 599
Reg: Aug 01, 2012
location unknow...
9,890
like
01/20/13 02:27 AM (13 years ago)
ok will do one by tonightand mail you
 
acschcs
Lost but trying
Profile
Posts: 118
Reg: Jan 16, 2013
United Kingdom
2,730
like
01/20/13 02:30 AM (13 years ago)
Be sure to :) I will be looking forward to watching it, and yeah, mail me on BT messages thank you!
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
01/20/13 07:25 AM (13 years ago)
@SuVinay Pawa, Looks like a good code. Great resource! LA
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
01/21/13 07:34 AM (13 years ago)
Nice work. Very interested in this as well- One java and two xml files to make a the plug-in, which I think I see you have pasted here. I can totally see making this into a plug-in from that code. Any chance you can post the link where you found this code? Maybe there are more options/details with it that might be useful?
 
Suvinay pawa
buzztouch Evangelist
Profile
Posts: 599
Reg: Aug 01, 2012
location unknow...
9,890
like
01/21/13 08:52 AM (13 years ago)
Ok..! but plz make it free and here is the link : http://vikaskanani.wordpress.com/2011/01/22/android-web-image-gallery/ but theres nothing else,thgh u can try,hey plz can yu make a video of how yu make that plugin coz i want to make some and need help with php and json..plz?
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
01/21/13 09:52 AM (13 years ago)
Thanks - exactly what I need. That is a fabouls find and a great coding blog. Trying to learn this Android plug-in stuff still myself. Love the source code link there - now I know I can cut/paste it properly to test. Proper syntax is everything. I'm not sure exactly when I'll get to this. It'd be a lot of trail and error still at this point. But this is pretty solid looking code and looks like some thing I can tackle. I believe there is a Mac Image gallery coming from Android - so I'm not looking to make a "market" plug-in out of it. If I do get it working I'd definately send you the code and my notes. php/json- Have you seen this post and does it make sense to you? https://www.buzztouch.com/forum/thread.php?tid=61A19811BADC9373C0AC264&command=isSearching&currentPage=1&topicTitle=json&createdBy=&repliedBy=&minViews=-1&maxViews=-1&minReplies=-1&maxReplies=-1&forumCategory= That is where it started to click for me.....
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
01/21/13 10:08 AM (13 years ago)
Hey @ATRAIN53, Great find! Do you have any more threads saved or bookmarked that can assist more in creating and implementing new plugins with the bt control panel? LA
 
Suvinay pawa
buzztouch Evangelist
Profile
Posts: 599
Reg: Aug 01, 2012
location unknow...
9,890
like
01/21/13 10:47 AM (13 years ago)
Awesome ! Thnkz mate
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.