Using Android's gallery app to select a photo to upload with httppost later. The photo gets uploaded but it's very low quality and size. I have been reading about setting the Bitmap.CompressFormat.PNG will make it lossless but it's taking the photo from 2000x1500 to 250x187.
Is it possible that I'm getting the thumbnail and not the actual image? I'm using Photos from Google.
Code
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data ) {
super.onActivityResult( requestCode, resultCode, data );
if ( resultCode == getActivity().RESULT_OK ) {
selectedImage = Uri.parse( data.getDataString() );
try {
final Bitmap v = DecodeUriImage.Decode( selectedImage, getActivity() );
ByteArrayOutputStream bao = new ByteArrayOutputStream();
v.compress( Bitmap.CompressFormat.PNG, 100, bao );
byte[] byte_arr = bao.toByteArray();
// Add image to array
images.set( selectedImageIndex, Base64.encodeToString( byte_arr, Base64.DEFAULT ) );
} catch ( FileNotFoundException e ) {
e.printStackTrace();
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
}
Result
The Bitmap.CompressFormat.PNG will compress the image but It depends on what kink of format you have used on the non-compressed image. You should use a vectorial image to get the best results because on the "compressing transfer" the immage loses the quality both in getting the image bigger on not.
There is no reason to put the selected image in a Bitmap for later use. Moreover you did not show what all is happening in DecodeUriImage.Decode( selectedImage, getActivity() );. Maybe it's quality is changed there. Instead realise you have a media path in selectedImage which you can convert to a real path to the filesystem. Save that path for later use.
If you use it later than just load the file in a byte array and encode base64. Do not use Bitmapfactory.
Related
I'm using following code from here. I want to compress image.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
After choose image from Camera, Gallery and Photos, i'm getting different type of paths in different devices based on OS type and device model. like:
1) /storage/emulated/0/Android/data/...
2) /raw//storage/emulated/0/mb/1511172993547.jpg
3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582
If path is like 1st url, this code is working fine. But if i get other types of images, then BitmapFactory.decodeFile() is giving null.
Is there any way to compress image in all types of devices and OS versions.
UPDATE :
To Open Picker :
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 1001);
After choosing image :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri imgUri = Uri.fromFile(new File(data.getData().getPath()));
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
dlgImageToPost.setImageBitmap(cmpBitmap);
...
}
For compression :
public static Bitmap compressUriQuality(Context mContext, Uri selectedImage) {
InputStream imageStream = null;
Bitmap bmp = null;
try {
imageStream = mContext.getContentResolver().openInputStream(
selectedImage);
bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 50, stream);
if (imageStream != null)
imageStream.close();
stream.close();
stream = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
Tv 2) /raw//storage/emulated/0/mb/1511172993547.jpg
That is not a path you would get if the user selected pictures from Gallery or Photos. And certainly it is no file system path you could use for .decodeFile(). How did you obtain it?
3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582
That is no valid content scheme path. How did you obtain it? And certainly cannot be used for .decodeFile().
i'm getting different type of paths
You will never get such paths if you 'act normal'. So what is it what you are doing? Elementary uri handling wrong?
using following code from here.
That is pretty dirty example code for a big part as you have perceived now.
any way to compress image in all types of devices and OS versions.
Of course. Just use the obtained selected uri directly. Open an InputStream for it and use .decodeStream() instead.
My god.. you are not using the uri directly.
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
Change to
Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, data.getData());
I had same issue, please add storage permission to your app.
Check this link for more info
Storage permission error in Marshmallow
I'm recording the photos taken by my application in the sqlite database, I know it's not very recommended anymore I need to transfer these photos to a central database that unifies all data from all applications.
The way I'm reducing and writing to sqlite bank is this way:
Android.Graphics.Drawables.BitmapDrawable bd = (Android.Graphics.Drawables.BitmapDrawable)imageView.Drawable;
Bitmap bitmap = bd.Bitmap;
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
byte[] ba = stream.ToArray();
string bal = Base64.EncodeToString(ba, Base64.Default);
Personally, I am editing my question because the problem is not in the select as I had thought it could be, but at the time of writing to the database. The code above works great when searching for a photo or image on the device. The problem is when it takes the photo for the application, then I get this error that is below.
I think I need to compress App.bitmap, which gets the shot taken right away. I just do not know how to do it yet. As I said the above code works great with the pictures taken by the device, but when I shoot through my application I get this error.
I'm going to post my OnActivityResult method so that you can help me figure out what this error is.
Error:
Java.Lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Searching on the internet this error is returning because of the size of the cursor that have a maximum of 2MB.
OnActivityResult Method:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
// Make it available in the gallery
try
{
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri = Uri.FromFile(App._file);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
// Display in ImageView. We will resize the bitmap to fit the display.
// Loading the full sized image will consume to much memory
// and cause the application to crash.
int height = Resources.DisplayMetrics.HeightPixels;
int width = imageView.Height;
App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
if (App.bitmap != null)
{
imageView.SetImageBitmap(App.bitmap);
// App.bitmap = null;
}
// Dispose of the Java side bitmap.
GC.Collect();
}
catch
{
}
try
{
if (resultCode == Result.Ok)
{
var imageView =
FindViewById<ImageView>(Resource.Id.imageView1);
imageView.SetImageURI(data.Data);
}
}
catch
{
}
}
Without fetching the photo, the select does not return the error quoted above. I'm counting on your help.
Use EncoderParameter to specify the level of compression for an image.
see https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter(v=vs.110).aspx
and https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.quality(v=vs.110).aspx
I tried to send an image to server from gallery, I compressed it with Base64.
I started an activity for gallery:
private void startGalleryActivity() {
Intent intent = new Intent();
intent.setType("image/*");
String selectPicture = getResources().getString(R.string.select_picture);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, GALLERY);
}
I received the result in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode == MainActivity.RESULT_OK) {
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
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]));
// Now we need to set the GUI ImageView data with data read from the picked file.
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(imagePath, options);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Server s = new Server("new");
s.send(encoded);
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
When I send the image to serve the size of it is 4 times higher. If I write the image after I read it, this is write with double size. Why do I have this overhead?
Why do I have this overhead?
In addition to the Base64 overhead itself, you are re-encoding the image as a PNG. If the image started as something else, like a JPEG, a PNG version of that image may be substantially larger.
Also, please delete the four lines preceded by // Let's read picked image path using content resolver. First, that code will fail on hundreds of millions of Android devices, because a Uri is not a file, and you cannot assume that you can get a local filesystem path for that data. Second, you do not need it, as BitmapFactory has a decodeStream() method that you can use with getContentResolver().openInputStream(pickedImage).
In addition, please do not call decode...() on BitmapFactory twice. Load the bitmap once. Use the bitmap both for the ImageView and for your uploading.
Calling compress on a PNG will not make your file smaller as it is already compressed. Converting a binary file to a text stream
will really make it big. To avoid less overhead by converting the PNG file
to text file, just send the file as is, as a byte array. And add the file length
in the header. You can use DataOutputStream to do this.
byte[] byteArray = byteArrayOutputStream .toByteArray();
ByteArrayOutputStream btOS = new ByteArrayOutputStream();
DataOutputStream dataOS = new DataOutputStreamEx(btOS);
dataOS.writeInt(byteArray.length); // length of file
dataOS.write(byteArray); // actual file
dataOS.write(0); // end of field
dataOS.close()
I don't know what you are using in the backend, but you can just read
the first 4 bytes of what you will receive and that will be the length
of you file. And use that length to read the entire file.
It will be approximately 37% larger:
Very roughly, the final size of Base64-encoded binary data is equal to
1.37 times the original data size
Source: http://en.wikipedia.org/wiki/Base64
You are using Bitmap and BitmapFactory to convert a small jpg file to a big png file. Why aren't you sending the jpg directly? So do NOT use Bitmap and BitmapFactory to begin with. You end up else with something that was not your file.
I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help
THanks
In onClick function I am using this code to pick image from gallery
Intent image = new Intent(Intent.ACTION_GET_CONTENT);
image.setType("Image/*");
startActivityForResult(image, 0);
And I have used following code to convert image that is in drawable to byte data.
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
data = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
bitmapdata = data.toByteArray();
Now how would i convert image from gallery to byte data.
Thanks
In onActivityResult you will receive the Uri to your selected image like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
Uri imageUri = data.getData();
//....
}
}
Then to retrieve it from the MediaStore you should use :
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
after that, you should process the Bitmap like you do it now.
I am using android inbuilt camera to take picture and then attaching the same picture to email, when i am testing this functionality in 1.6 device, i am able name the picture that to be taken by in built camera, but in 2.1, the picture is having a name i.e given by device,
How to give user defined name in 2.1 inbuilt camera images..
to avoid that problem i am saving the image manually but when i try to get the image back through intent as bitmap and then saving it to sd card using compress method
this methods handles result from inbuilt camera
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
File file = new File(Environment.getExternalStorageDirectory()
+ "/test.png");
switch (requestCode)
{
case PHOTO_ACTION:
if (resultCode == RESULT_CANCELED)
{
addPhoto = false;
Toast.makeText(this, "Canceled ", Toast.LENGTH_LONG).show();
break;
} else if (resultCode == RESULT_OK)
{
Bundle b = data.getExtras();
Bitmap bm = (Bitmap) b.get("data");
FileOutputStream out;
try
{
out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
scanPhoto(file.toString());
out = null;
addPhoto = true;
} catch (Exception e)
{
e.printStackTrace();
addPhoto = false;
}
but when i am storing like this i am getting two images. one with system given name and other with name given by me. but the image one which has user defined is having less resolution so i question is how to save the bitmap with more resolution with out compressing it ..
please help.... me
If you just want to save the bitmap without losing quality try using CompressFormat.PNG instead of JPEG, I've seen people having that problem before. Try changing:
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
with:
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
and see it it helps.
Apart from Rick answer above, make sure your are providing a URI to camera intent where it can save the full image, else it will return thumb image in data parameter of intent.
So it will be like:
Intent photoPickerIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File("Cache directory","img.png"); //== where you want full size image
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imgFile));
startActivityForResult(photoPickerIntent, PickPhoto);
This was the error that I was doing.