In my Android App, I have an option where a user can take his photograph or upload an image from his Gallery at the time of registration. This image, once successfully taken by camera or selected from Gallery, is shown in an ImageView. Below is the onClick of ImageView code that starts this process.
This is from Activity Layout.
<ImageView
android:id="#+id/imgUserImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:onClick="changePicture"
app:srcCompat="#drawable/profile_image_not_provided"
android:layout_below="#+id/textView2"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp" />
This is from Activity.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);
int exif_orn_a;
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
Uri selectedImage = data.getData();
imgProfilePicture.setImageURI(selectedImage);
imgProfilePicture.setImageBitmap(mphoto);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
else if (requestCode == PICK_IMAGE_GALLERY) {
Uri selectedImage = data.getData();
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Bitmap lphoto = null;
Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
}
if (cur != null) cur.close();
try {
lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imgProfilePicture.setImageURI(selectedImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have managed to get the Orientations properly. Now the problem is, how should I rotate the image before imgProfilePicture.setImageURI(selectedImage);.
I managed to find the solution. Please feel free to post a better solution, if you think this can be improvised.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView imgProfilePicture = (ImageView)findViewById(R.id.imgUserImage);
int exif_orn_a;
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
Uri selectedImage = data.getData();
imgProfilePicture.setImageURI(selectedImage);
imgProfilePicture.setImageBitmap(mphoto);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
else if (requestCode == PICK_IMAGE_GALLERY) {
Uri selectedImage = data.getData();
//Starting from here, I am checking the Orientation and storing the result in orientation variable.
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Bitmap lphoto = null;
Cursor cur = this.getContentResolver().query(selectedImage, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
Toast.makeText(Register.this, "Picture Orientation "+orientation, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Register.this, "Wrong Orientation", Toast.LENGTH_LONG).show();
}
if (cur != null) cur.close();
//This is the end of Orientation Check.
try {
lphoto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imgProfilePicture.setImageURI(selectedImage);
//Based on the Orientation Check result, I rotate the ImageView Image to make it straight.
if (orientation == 270) {
imgProfilePicture.setRotation(-90);
} else if (orientation == 0) {
imgProfilePicture.setRotation(0);
}
//This is the end of Image Rotation based on the Orientation Check Result.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
lphoto.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
byte[] byteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related
I have a problem in my app:
i must get photo from camera or gallery, show it in a small imageView and sent it to server in base64 format (i don't need save image as file in the phone).
i can take photo but with very bad quality!
The solutions found in internet don't work for me
Can you help me?
here is the code
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
bitmap_photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
userImage.setImageBitmap(newBitmap);
bitmap_photo = newBitmap;
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
userImage.setImageURI(selectedImage);
try {
bitmap_photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImage);
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap_photo,
70, 70, true);
bitmap_photo = newBitmap;
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
and this is the code to convert image to base64:
public String getBase64Image(){
if(bitmap_photo!=null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap_photo.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String base64image = Base64.encodeToString(byteArray, Base64.NO_WRAP);
return base64image;
} else {
return "";
}
}
thank you!
i have a problem with converting Bitmap to Base64. I want to post some images from camera or gallery. When i get them as image and i must convert to base64 but when i'm getting the string and testing with http://codebeautify.org/base64-to-image-converter to see my image. Result looks very bad quality. How can i fix this pls help me ?
Here is my code:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode)
{
case 0:
if(resultCode == RESULT_OK)
{
Bundle extras = imageReturnedIntent.getExtras();
Bitmap selectedImage = (Bitmap)extras.get("data");
secilenImage.setImageBitmap(selectedImage);
String encodedImage;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try
{
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] b = stream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
images.add(i, encodedImage);
Log.d("StringForCamera: ", encodedImage);
stream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
secilenImage.setImageBitmap(selectedImage);
secilenImage.setEnabled(false);
}
break;
case 1:
if(resultCode == RESULT_OK)
{
Uri imageUri = imageReturnedIntent.getData();
Bitmap selectedImage = null;
String encodedImage;
try
{
selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
images.add(i, encodedImage);
Log.d("StringForGallery: ",encodedImage);
}
catch(IOException e)
{
e.printStackTrace();
}
secilenImage.setImageBitmap(selectedImage);
secilenImage.setEnabled(false);
}
break;
}
++i;
}
(Bitmap)extras.get("data");
That is only a thumbnail of the image. Hence the low quality.
In the below code I am getting an image from sd card , and sending the image through an intent,
however I want to change the code to send the path of the image , and not the image itself
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }
and here I am receiving the image
byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);
Also I tried this but it didn't work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
Intent i = new Intent(this, AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } }}
but how to receive the uri path
geturi().getintent() ?
and then get the image
edit--
I got error nullpointerexception uri string here
img.setImageURI(Uri.parse(imagePath ));
this is the code
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
out.recycle();
/* byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
// bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);*/
// ImageView imageview = (ImageView) findViewById(R.id.imageView);
img.setImageBitmap(out);
i am sending intent like this
filePath = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
AddImage Activity
onCreate(...)
{
....
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView1);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
img.setImageBitmap(out);
bitmap.recycle();
....
}
I'm trying to load an image from the gallery, but toSave.recycle() doesn't work.
I mean when toSave.recycle() is called the screen becomes white, the app doesn't crash but the image is not shown.
Do you have any suggestions?
Here the code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LOAD_IMAGE_RESULT && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
postImage.setImageBitmap(bitmap);
saveImage(bitmap);
cursor.close();
}
}
Here the method that converts bitmap image and save it in Firebase.
public void saveImage(Bitmap toSave){
if (toSave!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
toSave.compress(Bitmap.CompressFormat.JPEG, 50, stream);
toSave.recycle();
byte[] byteArray = stream.toByteArray();
String imageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
simplepostRef.child("image").setValue(imageString);
}
Thank you a lot!
bitmap.recycle() is working in your case. You just dont need to recycle the bitmap because you are showing this bitmap in a imageview.Have a look at the docs.
Here is my code i m writing for opening gallery and camera in my application . App crashes when back btutton is pressed without setting
image. Suggest me solutions to it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
// this is image view where you want to setimage
bmp = (Bitmap) data.getExtras().get("data");
ivKidsProfilePicture.setImageBitmap(bmp);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, baos);
childpic = baos.toByteArray();
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
masterDatabaseAdapter.insertProfilePicture(childpic);
}
else {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if (bmp != null && !bmp.isRecycled()) {
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
ivKidsProfilePicture.setBackgroundResource(0);
ivKidsProfilePicture.setImageBitmap(bmp);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, baos);
childpic = baos.toByteArray();
}
masterDatabaseAdapter.insertProfilePicture(childpic);
}