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.
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 am creating a app in which on button click i open the default camera to camera a image and save that image.But when ever i am viewing that image,the quality of that image is very poor.I dont know why this is happening???
Code
if (userSelection.contains("Camera")) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
btChooseDoc.setText("Choose File");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);{
Bitmap camera;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {
camera = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(getActivity(), camera);
strFilePath = getPath(getActivity(), tempUri);
file = new File(strFilePath);
camera.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// camera = Bitmap.createScaledBitmap(camera, 150, 150, false);
image.setImageBitmap(camera);
date = sdf.format(new Date());
}
Change
galleryImage.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
to
galleryImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
The second argument in the compress method is the quality of the image. If you give a small value it will decrease the image quality to compress it.
I am doing an android app, in which I have a functionality that, I need to open camera and take a picture. After taking picture onActivityResult is not called. my screen remains only in camera state.
My code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaFile = new File(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
Uri imageUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
onActivityResult code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null && !data.toString().equalsIgnoreCase("Intent { }"))
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
}
onActivityResult is probably called, but the statements inside your if statement and switch/case statements are not being executed, because they are never reached.
The code below includes two methods takePhoto and onActivityResult, is very short and works perfectly. Take a look at it, it probably will help you!
public void takePhoto(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
You can run your application in debug mode, after putting a breakpoint in onActivityResult method,
if it's stopping there after getting from the Camera then it's being called successfully.
and you could just change the if statement you're using making your code like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null && resultCode = RESULT_OK)
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
}
and note that it is more recommended to using final ints as the request code instead of rewriting them everytime they're needed
like :
private final int START_CAMERA_REQUEST_CODE = 1;
I think this may be help you
Follow Below Code step by step and compare with your code to avoid null exception
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
1 OnActivity Result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1)
onCaptureImageResult(data);
}
}
2
onCaptureImageResult
public void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
Uri tempUri = getImageUri(getApplicationContext(), thumbnail); // call getImageUri
File finalFile = new File(getRealPathFromURI(tempUri));
file_path = finalFile.getPath().toString(); // your file path
imgview.setImageBitmap(thumbnail);
// this code will get your capture image bitmap}
3 getImageUri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); // your capture image URL code
return Uri.parse(path);
}
Using this code your screen will not remain more on camera state it will finish and image will be load as well you get image URL.
Bitmap mBitmap = BitmapFactory.decodeFile(imgPath);
pass Image path which you create from URI.
Probably, your activity is reloaded after camera is finished, but your code is not prepared to such situation.
See Android startCamera gives me null Intent and ... does it destroy my global variable?.
I think onActivity is called but it doesn't go into the if condition
Check only for null in if condition:
if (data != null )
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
From where you are calling startActivityForResult(intent, 1),
and where you Overriding onActivityResult() ?
I mean if you call startActivityForResult() from Frgment then you should override onActivityResult() in fragment it self, same applies to Activity also.
I am trying to capture an image using Android Camera Intent. Camera intent returns Byte Array and when I saved the byte array as a Bitmap, I am getting a very small image instead of getting an Image based on current camera settings (1024 Pixels currently set in the android mobile camera).
Usally i will get file path from the camera intent but somehow i am not getting from this device, so i am creating the bitmap from the byte returned by camera intent.
Anybody knows why is this and how to solve this issue. Thanks.
The below is the java code block I am using.
private Intent cameraIntent = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
if ( data != null)
{
Bitmap myImage = null;
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
byte[] byteArray = stream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
myImage = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length, options);
fileOutputStream = new FileOutputStream(sPath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}
}
}
}
The data.getExtras("data") only returns a thumbnail image. To obtain the full-sized image, you need to pass the camera intent a file in which to store that image and later retrieve. A rough example follows.
Start the intent:
File dir = new File(Environment.getExternalStorageDirectory() + "/dcim/myappname");
File mFile = File.createTempFile("myImage", ".png", dir);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Inside onActivityResult:
if (resultCode == RESULT_OK) {
Bitmap bm = BitmapFactory.decodeFile(mFile.getAbsolutePath());
}
// do whatever you need with the Bitmap
Keep in mind that mFile will have to be global or somehow persisted so that it may be called upon where necessary.
I am new to this site and android. how to save the camera's picture into specific folder and save the picture's name into sqlite databse also.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.camera);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
onActivityResult(1337, 0, cameraIntent);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == CAMERA_PIC_REQUEST) {
}
I opened camera activity. How to get the picture name & save it to particular location?
please help me on this.
thank you in advance.
I'm calling camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
and saving to sd card like this
final ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(100, 100));
image2 = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image2);
String incident_ID = IncidentFormActivity.incident_id;
//l2.addView(img);
imagepath="/sdcard/RDMS/"+incident_ID+ x + ".png";
File file = new File(imagepath);
try {
bm = Bitmap.createScaledBitmap( image2,400, 300, true);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(CompressFormat.PNG, 90, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"yourfirst error message is "
+ e.toString(), 1000).show();
}
You may need to change the line
if (resultCode == CAMERA_PIC_REQUEST) {
to
if (requestCode == CAMERA_PIC_REQUEST) {
You are already saving the picture. You provided the location in MediaStore.EXTRA_OUTPUT.
Link
Doing a DB you will have to have a look at this link is a little bit to much to explain