Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This function is called when the add player picture button is clicked.
- * It accesses the devices gallery and the user can choose a picture
- * from the gallery.
- * Or if the user chooses to take a picture with the camera, it handles that
- */
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case TAKE_PICTURE:
- if (resultCode == Activity.RESULT_OK) {
- Uri selectedImage = imageUri;
- getContentResolver().notifyChange(selectedImage, null);
- ContentResolver cr = getContentResolver();
- this.picPath = selectedImage.getPath();
- Bitmap bitmap;
- try {
- bitmap = android.provider.MediaStore.Images.Media
- .getBitmap(cr, selectedImage);
- imageView = (ImageView) findViewById(R.id.imagePlayer);
- imageView.setImageBitmap(bitmap);
- Toast.makeText(this, selectedImage.toString(),
- Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
- .show();
- Log.e("Camera", e.toString());
- }
- }
- Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50
- // below 3 line of code will come instead of
- //imageView.setImageBitmap(bitmap);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
- imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));
- // please define following two methods in your activity
- public Bitmap decodeSampledBitmapFromByte(byte[] res,
- int reqWidth, int reqHeight) {
- // First decode with inJustDecodeBounds=true to check dimensions
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeByteArray(res, 0, res.length,options);
- // Calculate inSampleSize
- options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
- // Decode bitmap with inSampleSize set
- options.inJustDecodeBounds = false;
- return BitmapFactory.decodeByteArray(res, 0, res.length,options);
- }
- public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
- // Raw height and width of image
- final int height = options.outHeight;
- final int width = options.outWidth;
- int inSampleSize = 1;
- if (height > reqHeight || width > reqWidth) {
- if (width > height) {
- inSampleSize = Math.round((float)height / (float)reqHeight);
- } else {
- inSampleSize = Math.round((float)width / (float)reqWidth);
- }
- }
- return inSampleSize;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement