Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 5    Views: 55

ehwright
Aspiring developer
Profile
Posts: 57
Reg: Feb 21, 2012
Perth
2,370
07/19/13 02:04 AM (12 years ago)

Screen Menu Image Android URL images

I've been having some issues with the screen menu image plugin. The plugin works fine with local files, however when icon images are referenced via a url, on newer android devices the image is not displayed. On older devices, the image displays just fine. I had a dig around the code and found out that an exception is thrown because the downloading is not happening asynchronously. I added a image downloader for those images so that it happens in the background and the problem went away. If anyone wants to know how to do this, just ask and I'll share what I did. ( I'm not saying it's pretty though)
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
07/19/13 02:42 AM (12 years ago)
Please share what you did, so that we can all learn for the future.
 
ehwright
Aspiring developer
Profile
Posts: 57
Reg: Feb 21, 2012
Perth
2,370
like
07/19/13 02:53 AM (12 years ago)
sure, remember it's not pretty..... In wb_screen_menuImage, around line 665 I have introduced some changes, with comments: //setup icon view... Drawable icon = null; String isdownloaded = "false"; //iconName or iconURL? String tmpIconName = BT_strings.getJsonPropertyValue(tmpItem.getJsonObject(), "iconName", ""); String tmpIconURL = BT_strings.getJsonPropertyValue(tmpItem.getJsonObject(), "iconURL", ""); if(tmpIconName.length() > 1){ //BT_debugger.showIt("icon name length is" + tmpIconName.length()); //BT_debugger.showIt("icon name is : " + tmpIconName); icon = BT_fileManager.getDrawableByName(tmpIconName); }else{ if(tmpIconURL.length() > 0){ //BT_debugger.showIt("icon url is : " + tmpIconURL); //does the image exist in the cache? String tmpCachedFileName = BT_strings.getSaveAsFileNameFromURL(tmpIconURL); if(BT_fileManager.doesCachedFileExist(tmpCachedFileName)){ icon = BT_fileManager.getDrawableFromCache(tmpCachedFileName); }else{ //Bitmap b = BT_fileManager.getImageFromURL(tmpIconURL); //changing to work out the image downloading problem //july 19 2013 BT_debugger.showIt("trying the async task thingo"); new DownloadImageTask(iconView).execute(tmpIconURL); isdownloaded = "true"; //adding the new reference to asynchronous task } } } //Get the screen density of the device float scale = getResources().getDisplayMetrics().density; BT_debugger.showIt(activityName + "This is what scale is: " + scale); BT_debugger.showIt("this is what isdownloaded is:" + isdownloaded); ///Set location for text if there is or is no icon if(icon == null ){ BT_debugger.showIt("icon is null"); if (isdownloaded == "false"){ BT_debugger.showIt("isdownloaded is false"); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.leftMargin =(int)( 5 * scale); lp.rightMargin = (int)( 45 * scale); lp.addRule(RelativeLayout.CENTER_VERTICAL); titleView.setLayoutParams(lp); descriptionView.setLayoutParams(lp); }else{ BT_debugger.showIt("downloaded is true"); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.leftMargin = (int)( 60 * scale); lp.rightMargin = (int)( 45 * scale); lp.addRule(RelativeLayout.CENTER_VERTICAL); titleView.setLayoutParams(lp); descriptionView.setLayoutParams(lp); } }else { BT_debugger.showIt("icon is not null"); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.leftMargin = (int)( 60 * scale); lp.rightMargin = (int)( 45 * scale); lp.addRule(RelativeLayout.CENTER_VERTICAL); titleView.setLayoutParams(lp); descriptionView.setLayoutParams(lp); } //set icon iconView.setImageDrawable(icon); and the new class file called DownloadImgeTask.java is this: package com.community2go; import java.io.InputStream; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.communitytogo.wandinaps.R; public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { private ProgressDialog mDialog; private ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } protected void onPreExecute() { //mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true); } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", "image download error"); Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { //set image of your imageview bmImage.setImageBitmap(result); //close // mDialog.dismiss(); } }
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
07/19/13 03:12 AM (12 years ago)
Thank you! Stored in my code box. Cheers! -- Smug
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
07/19/13 04:42 AM (12 years ago)
Thank you veru much. Pretty or not pretty, it doesn't matter. It is very useful!
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
07/19/13 12:19 PM (12 years ago)
Sweet and saved! LA
 

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.