Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomSelfieListviewAdapter extends BaseAdapter
- {
- private Activity activity;
- private ArrayList<HashMap<String, String>> data;
- private static LayoutInflater inflater=null;
- public CustomSelfieListviewAdapter(Activity a, ArrayList<HashMap<String, String>> d)
- {
- activity = a;
- data=d;
- inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public int getCount()
- {
- return data.size();
- }
- public Object getItem(int position)
- {
- return position;
- }
- public String getItemPath(int position)
- {
- HashMap<String, String> item = new HashMap<String, String>();
- item = data.get(position);
- String mpath = item.get("pic");
- return mpath;
- }
- public long getItemId(int position)
- {
- HashMap<String, String> item = new HashMap<String, String>();
- item = data.get(position);
- int lvID = Integer.parseInt(item.get("lv_id"));
- return lvID;
- }
- public View getView(int position, View convertView, ViewGroup parent)
- {
- View vi=convertView;
- ViewHolder holder = new ViewHolder();
- HashMap<String, String> item = new HashMap<String, String>();
- item = data.get(position);
- String lvTitle = item.get("lv_title");
- String lvImages = item.get("lv_img");
- //vi = inflater.inflate(R.layout.activity_listview_selfie_item, null);
- //TextView list_item = (TextView)vi.findViewById(R.id.list_item);
- //list_item.setText(lvTitle);
- String picpath = lvImages;
- Log.i("Pic URL", picpath);
- Uri imgUri = Uri.fromFile(new File(picpath));
- Bitmap bm = getBitmap(imgUri);//BitmapFactory.decodeFile(picpath, options);
- //ImageView img = (ImageView)vi.findViewById(R.id.picture);
- //img.setImageBitmap(bm);
- if(vi == null)
- {
- vi = inflater.inflate(R.layout.activity_listview_selfie_item, null);
- holder.text = (TextView)vi.findViewById(R.id.list_item);
- holder.icon = (ImageView)vi.findViewById(R.id.picture);
- vi.setTag(holder);
- }
- final int finalPosition = position;
- if(item != null)
- {
- holder = (ViewHolder)vi.getTag();
- holder.text.setText(lvTitle);
- holder.icon.setImageBitmap(bm);
- }
- return vi;
- }
- private Bitmap getBitmap(Uri uri)
- {
- InputStream in;
- try
- {
- final int IMAGE_MAX_SIZE = 200000; //0.2MP 1.2MP
- in = this.activity.getContentResolver().openInputStream(uri);
- // Decode image size
- BitmapFactory.Options o = new BitmapFactory.Options();
- o.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(in, null, o);
- in.close();
- int scale = 1;
- while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE)
- {
- scale++;
- }
- Log.d("TAG", "scale = " + scale + ", orig-width: " + o.outWidth + ",orig-height: " + o.outHeight);
- Bitmap b;
- in = this.activity.getContentResolver().openInputStream(uri);
- if (scale > 1)
- {
- scale--;
- // scale to max possible inSampleSize that still yields an image
- // larger than target
- o = new BitmapFactory.Options();
- o.inSampleSize = scale;
- b = BitmapFactory.decodeStream(in, null, o);
- // resize to desired dimensions
- int height = b.getHeight();
- int width = b.getWidth();
- Log.d("TAG", "1th scale operation dimenions - width: " + width + ",height: " + height);
- double y = Math.sqrt(IMAGE_MAX_SIZE / (((double) width) / height));
- double x = (y / height) * width;
- Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
- b.recycle();
- b = scaledBitmap;
- System.gc();
- }
- else
- {
- b = BitmapFactory.decodeStream(in);
- }
- in.close();
- Log.d("TAG", "bitmap size - width: " + b.getWidth() + ", height: " + b.getHeight());
- return b;
- }
- catch (IOException e)
- {
- Log.e("TAG", e.getMessage(), e);
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement