Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. public class CustomSelfieListviewAdapter extends BaseAdapter
  2. {
  3.     private Activity activity;
  4.     private ArrayList<HashMap<String, String>> data;
  5.     private static LayoutInflater inflater=null;
  6.  
  7.     public CustomSelfieListviewAdapter(Activity a, ArrayList<HashMap<String, String>> d)
  8.     {
  9.         activity = a;
  10.         data=d;
  11.         inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  12.     }
  13.  
  14.     public int getCount()
  15.     {
  16.         return data.size();
  17.     }
  18.  
  19.     public Object getItem(int position)
  20.     {
  21.         return position;
  22.     }
  23.  
  24.     public String getItemPath(int position)
  25.     {
  26.         HashMap<String, String> item = new HashMap<String, String>();
  27.         item = data.get(position);
  28.         String mpath = item.get("pic");
  29.         return mpath;
  30.     }
  31.  
  32.     public long getItemId(int position)
  33.     {
  34.         HashMap<String, String> item = new HashMap<String, String>();
  35.         item = data.get(position);
  36.  
  37.         int lvID  = Integer.parseInt(item.get("lv_id"));
  38.         return lvID;
  39.     }
  40.  
  41.     public View getView(int position, View convertView, ViewGroup parent)
  42.     {
  43.         View vi=convertView;
  44.         ViewHolder holder = new ViewHolder();
  45.         HashMap<String, String> item = new HashMap<String, String>();
  46.         item = data.get(position);
  47.  
  48.         String lvTitle  = item.get("lv_title");
  49.         String lvImages = item.get("lv_img");
  50.  
  51.         //vi = inflater.inflate(R.layout.activity_listview_selfie_item, null);
  52.         //TextView list_item = (TextView)vi.findViewById(R.id.list_item);
  53.         //list_item.setText(lvTitle);
  54.  
  55.  
  56.         String picpath = lvImages;
  57.         Log.i("Pic URL", picpath);
  58.  
  59.         Uri imgUri = Uri.fromFile(new File(picpath));
  60.         Bitmap bm =  getBitmap(imgUri);//BitmapFactory.decodeFile(picpath, options);
  61.         //ImageView img = (ImageView)vi.findViewById(R.id.picture);
  62.         //img.setImageBitmap(bm);
  63.  
  64.         if(vi == null)
  65.         {
  66.             vi = inflater.inflate(R.layout.activity_listview_selfie_item, null);
  67.             holder.text = (TextView)vi.findViewById(R.id.list_item);
  68.             holder.icon = (ImageView)vi.findViewById(R.id.picture);
  69.             vi.setTag(holder);
  70.         }
  71.  
  72.         final int finalPosition = position;
  73.  
  74.  
  75.         if(item != null)
  76.         {
  77.             holder = (ViewHolder)vi.getTag();
  78.             holder.text.setText(lvTitle);
  79.             holder.icon.setImageBitmap(bm);
  80.         }
  81.  
  82.         return vi;
  83.     }
  84.  
  85.     private Bitmap getBitmap(Uri uri)
  86.     {
  87.         InputStream in;
  88.         try
  89.         {
  90.             final int IMAGE_MAX_SIZE = 200000; //0.2MP 1.2MP
  91.             in = this.activity.getContentResolver().openInputStream(uri);
  92.  
  93.             // Decode image size
  94.             BitmapFactory.Options o = new BitmapFactory.Options();
  95.             o.inJustDecodeBounds = true;
  96.             BitmapFactory.decodeStream(in, null, o);
  97.             in.close();
  98.  
  99.  
  100.             int scale = 1;
  101.             while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE)
  102.             {
  103.                 scale++;
  104.             }
  105.             Log.d("TAG", "scale = " + scale + ", orig-width: " + o.outWidth + ",orig-height: " + o.outHeight);
  106.  
  107.             Bitmap b;
  108.             in = this.activity.getContentResolver().openInputStream(uri);
  109.             if (scale > 1)
  110.             {
  111.                 scale--;
  112.                 // scale to max possible inSampleSize that still yields an image
  113.                 // larger than target
  114.                 o = new BitmapFactory.Options();
  115.                 o.inSampleSize = scale;
  116.                 b = BitmapFactory.decodeStream(in, null, o);
  117.  
  118.                 // resize to desired dimensions
  119.                 int height = b.getHeight();
  120.                 int width = b.getWidth();
  121.                 Log.d("TAG", "1th scale operation dimenions - width: " + width + ",height: " + height);
  122.  
  123.                 double y = Math.sqrt(IMAGE_MAX_SIZE / (((double) width) / height));
  124.                 double x = (y / height) * width;
  125.  
  126.                 Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
  127.                 b.recycle();
  128.                 b = scaledBitmap;
  129.  
  130.                 System.gc();
  131.             }
  132.             else
  133.             {
  134.                 b = BitmapFactory.decodeStream(in);
  135.             }
  136.             in.close();
  137.  
  138.             Log.d("TAG", "bitmap size - width: " + b.getWidth() + ", height: " + b.getHeight());
  139.             return b;
  140.         }
  141.         catch (IOException e)
  142.         {
  143.             Log.e("TAG", e.getMessage(), e);
  144.             return null;
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement