Sorry for my english. I get image from camera, but i have bad quality image from camera. Bellow my code:
Bitmap thumbnail = (Bitmap) imageReturnedIntent.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
image.setImageBitmap(thumbnail);
I get image from camera, but i have bad quality image from camera.
Apparently, you are not including EXTRA_OUTPUT in your Intent, and therefore you are only getting the thumbnail. Quoting the documentation for ACTION_IMAGE_CAPTURE:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
Related
While I select the image from the gallery and shows it in ImageView. The image quality is all right. But, uploading an image on the server, it lost quality and become a blur. I obtain the image from the camera by this code.
private void onCaptureImageResult(Intent data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File destination = new File(
Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg"
);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
Then, I did this work-
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
and used this function to compress my selected photo. But, it makes the loss of that image quality and image become a blur on the server. Why am I facing this problem?
You could use the .png format as it's lossless and doesn't reduce the image quality. On the other hand the .jpeg format is just the opposite of this.
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte, Base64.DEFAULT);
}
You did not show the intent to start a Camera app.
But you did it in such a way that you only got a thumbnail of the picture taken.
Change the intent. Add an uri where the camera app can save the full picture.
There are 783 examples of such an intent on stackoverflow and even more on the internet.
Ok i'm completely editing this post... I have made it so that I can save the file path to my data base. this works and is saved as /storage/emulated/0/1508blah blah.jpg . Now i cannot get my code to read this item back into a picture.
imagePhoto = (ImageView)findViewById(R.id.detail_recipe_image);
Toast.makeText(this, recipe.image, Toast.LENGTH_SHORT).show();
Bitmap bmp = BitmapFactory.decodeFile(String.valueOf(recipe.image));
imagePhoto.setImageBitmap(bmp);
am I missing something here? cause the Toast Is reading the recipe.image just fine and is displaying the path. why Is the rest not displaying the image?
Storage Code
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
String picturePath = destination.toString();
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textImagePath.setText(picturePath.toString());
ImageView img = (ImageView)findViewById(R.id.addphotoview);
img.setImageBitmap(thumbnail);
}
Adding in the files paths seem to be the best solution to the problem i am having so that you #ModularSynth for your help with this. Always making sure all the info is your code to make the file paths work helps.
I need my App to take a photo using the camera, show it in my Activities ImageView, and then sent it to a server using an HttpClient. So far, so good. Unfortunately, I stumbled upon the well described MemoryOutOfBoundsException. So I want to compress my image using JPG or PNG.
Now - after some excessive googling - do I get this right:
a) The camera will always output an uncompressed Bitmap, which is directly written to the file system. I.e. like this:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.imageTempFile = new File(android.os.Environment.getExternalStorageDirectory(), "myTempFileName"); // write the camera output to a tmpFile
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.imageTempFile)); // link the tmpFile to a member for convenience later on
startActivityForResult(cameraIntent, CAMERA_REQUEST);
So there is no way to resize / compress it right away?
b) If I want to display an image in a ImageView, I need to pass a Bitmap to it using ImageView.setImageBitmap(Bitmap bm). So showing the result is extremely memory consuming...!?
c) If I want to alter the Bitmap (resize / compress), I need to read it from this file into memory using a BitmapFactory
d) Now I can resize the Bitmap using Bitmap.createScaledBitmap()
e) But if I want to compress the image, I need to write it back to the file system using an OutputStream via Bitmap.compress()...
f) So for the task of taking an image, resizing it, showing it, and then sending it to a server, I need to actually write it to a file, then read it, then write it to a file again, then read it - and then send it? WTF? I there no easier way?
PS: Here's my code for b) to e):
// c) read the Bitmap from file
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(this.imageTempFile.getAbsolutePath(), bitmapOptions);
// d) do some resizing
bitmap = Bitmap.createScaledBitmap(bitmap, (int) mywidth, (int) myheight, true);
// e) compress
OutputStream out = new ByteArrayOutputStream(50);
this.imageTempFile.delete();
File file = new File(this.imageTempFile.getAbsolutePath());
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// NOW we could read it again from the file to send it afterwards...
Bitmap newBitmap = BitmapFactory.decodeFile(this.imageTempFile.getAbsolutePath(), bitmapOptions);
I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.
I want to replace the bitmap image after rotating it, from the original image stored any where in the mobile. I have rotated an image at 90 degree but I'm unable to replace it from that original image.
MediaStore.Images.Media.insertImage(getContentResolver(), rotatedBitmapImage, imageName, "");
I have used above code but it is storing image in DCIM -> camera folder. But I want to replace it from URI of the original image.
I have got answer of this question. Inspite of using above code I have used:
OutputStream fOut = null;
File file = new File(imagePath);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
//---------------Used Media Scanner-----------
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));