I'm playing with image uploading on android and the first method I encountered to do so is using the Base64 codification, however I noticed that the image quality is drastically lowered.
Do you have any suggestion or know other ways to upload an image to a MongoDB keeping a good quality?
Code I'm actually using:
public static String toBase64(Bitmap image) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
}
public static Bitmap fromBase64(String encodedImage) {
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}
And here's what I use to capture the image:
private void captureImage() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap image = (Bitmap) data.getExtras().get("data");
uploadImage(image);
}
}
Related
Hy,,, this has been confused me for a while since what I intended to do with my app was just to capture images and upload it without saving it into the gallery. But what happen is the opposite.
This is how I call the camera function
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_01);
And this is how I handle the request
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == CAMERA_01){
if(resultCode == Activity.RESULT_OK){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] byte_arr = output.toByteArray();
i11a1 = Base64.encodeToString(byte_arr, Base64.DEFAULT);
((SiteBefore)getActivity().getApplication()).seti11a1(i11a1);
BitmapDrawable ob = new BitmapDrawable(getResources(),bitmap);
img11a1.setBackgroundDrawable(ob);
}else if(resultCode == Activity.RESULT_CANCELED){
}
}
}
This code is written inside the fragment class. This cause the image being saved into the gallery. Anyone know what causes this?
I've been developed app to take a picture to be saved in gallery. I've searched online and the easiest way that I could practice was to use data.getExtras().get("data") . So below are the code to take picture from camera. Note that I'm using fragment in this class.
img22a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_01);
}
});
Get the captured image and convert it to string
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_01) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
byte[] byte_arr = output.toByteArray();
((SiteProgress) getActivity().getApplication()).seti22a1(Base64.encodeToString(byte_arr, Base64.DEFAULT));
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
img22a1.setBackgroundDrawable(ob);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
I save the string into global variable so another activity can access it.
In another activity, I have a button to save the image into folder/gallery by accessing the image string.
for(int i=0;i<=namefile.length;i++){
Bitmap[] bitmap = new Bitmap[namefile.length];
FileOutputStream outputStream = new FileOutputStream(String.valueOf(namefile[i]));
bitmap[i] = BitmapFactory.decodeByteArray(decodedString[i],0,decodedString[i].length);
bitmap[i].compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), namefile[i].getAbsolutePath(), namefile[i].getName(), namefile[i].getName());
}
It worked to save the image into folder/gallery....But the quality of image was very low...I could barely see text in the image.
They said data.getExtras().get("data") supposed to return thumbnail and not the actual image. What I want here is to get the full image and not the thumbnail one.
Any answer will be very appreciated.
From: Android - How to get the image using Intent data
if(data != null)
{
Uri selectedImageUri = data.getData();
filestring = selectedImageUri.getPath();
Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);
System.out.println(String.format("Bitmap(CAMERA_IMAGES_REQUEST): %s", thumbnail));
System.out.println(String.format("cap_image(CAMERA_IMAGES_REQUEST): %s", cap_image));
cap_image.setImageBitmap(thumbnail);
}
I have an application with one simple button which allows to take a picture. I want to send this picture to a matlab server. Here is my code:
button = (Button) findViewById(R.id.uploadButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(MainActivity.this,"Try Toast",Toast.LENGTH_LONG).show();
//Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//startActivity(intent);
Intent cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
And this the others method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
//did the user choose ok? If so, the code inside these curly braces will execute.
if (resultCode == RESULT_OK){
if (requestCode == CAMERA_REQUEST){
//we are hearing back from the camera
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
//at this point we have the image from the camera.
//imgSpecimenPhoto.setImageBitmap(cameraImage);
Bitmap bJPGcompress = codec(cameraImage, Bitmap.CompressFormat.JPEG, 100);
imgSpecimenPhoto.setImageBitmap(bJPGcompress);
}
}
}
private static Bitmap codec(Bitmap img, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img.compress(format, quality, baos);
byte[] array = baos.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
I'm trying to upload an image to server in my Android application by converting it to a base64 string. In this case when I try to upload an image taken from my camera the quality of my image is largely reduced and is very much blurred. Can you please help me overcome this issue.
Here's my code:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Log.i("camera intent", "camera intent");
bitmap = (Bitmap) data.getExtras().get("data");
bitmap = Bitmap.createScaledBitmap(bitmap, 720, 1280, true);
viewImage_imageView.setImageBitmap(bitmap);
UploadImage_textView.setEnabled(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
String file = Base64.encodeToString(data, 0);
Log.i("base64 string", "base64 string: " + file);
new ImageUploadTask(file).execute();
}
}
bitmap = (Bitmap) data.getExtras().get("data");
This will give the thumbnail of the image taken by camera that's why you are seeing blurred image
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri tempURI = Uri.fromFile(<file path where you want to save image>);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
then for this path you will get the actual image click by camera now upload that image to server.
What i am having: i am using a camera to capture a image and i want to display it in a listview by passing bitmap to adapter in android
private void startCameraCapture() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1024);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent myData) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1024) {
.............. mycode
}
}
What i want to do :: i want to convert the data i received as myData into a bitmap in android
Convert byte[] to bitmap
Bitmap bmp = myData.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();