Why do I get IOException while creating images using lwuit? - android

I am getting IOException when I try to create image in lwuit.
the following is the code :
InputStream is = img.getResourceAsStream();
Image img = Image.createImage(is);
the actual size of images are 1 mb and above. Where I am able to create images of 100 or 200 kb in size with the same code.
Thanks.

Yes the size of your images is the reason
try to resize your images using this site
http://www.picresize.com/

Related

what is the best way to work with Images in SqLite android?

I am working with a customizable database with pictures. Right now I am taking pictures as it is from the sdcard and encoding it in base64 String and then putting it in the database. but whenever I am trying decoding it and showing it in my view, I am getting Out of memory error. Can any one one tell me what is the best procedure to do it? Shall I change the size of the pictures before encoding it?
I want to re-size all of the pictures into 512*512.
Image to Base64 is very heavy operation in android. Consider saving the images on the external/internal memory and save the file path in the sqlite database.
You can convert your image to byte array then store values in sql by using BLOB type and vice versa.
As you mentioned you want to resize the images to 512*512, you can scale the image using below code,
Create bitmap from captured image and then use below line
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 512, 512, false);
It will give you a smaller image, you can also consider compressing the image to reduce in size,
OutputStream imagefile = new FileOutputStream("/your/file/name.jpg");
// Write 'bitmap' to file using JPEG and 50% quality hint for JPEG:
bitmap.compress(CompressFormat.JPEG, 50, imagefile);
Now, you have two options,
Save the scaled and compressed image into a file and save the path of that file in db. (Better way)
Convert the scaled and compressed image to base64 string and save in db.
Althought base64 is , as many answers said, a heavy operation for android, if done properly, it should not be a problem.
There are many reasons a bitmap could be required to be saved to a DB , (photo of a invoice ticket, for example?), and this is the way i do it.
first, create a new , smaller bitmap like #Swapnil commented.
and second, correctly use the bitmap transformation methods, i've been using these (look below) two so far and haven't had any memory issue on many different devices.
link to my BitmapUtils transformation methods

Failed uploading larger (size) images from an android app to database

I am calling my REST api made using jersey with POST request and I have put my file as a multipart entity
File file=new File(mImageUri.getPath());
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("image", new FileBody(file));
My code is working when the size of image is relatively small (around less than 1 mb) but is getting failed for larger images.
You have to shrink image to a thumbnail size.
According to android documentation:
For example, it’s not worth loading a 1024x768 pixel image into memory
if it will eventually be displayed in a 128x96 pixel thumbnail in an
ImageView.
Read this SO post.
It seems server side issue. Check on serverside if there is any limit set because by default there is limit inside server side for upload image or video like 2mb or 3 mb.

How to retrieve the video thumbnail in Android

My sdcard video folder contains number of .mp4 video files.
According to my requirement I want to list down those video files in my android application listview with their "Thumbnail" and "Name". BTW I plan to use Picasso or Universal image loader for image caching. Please tell me anyone know how to do?
import android.provider.MediaStore.Video.Thumbnails;
You can get two preview thumbnail sizes from the video:
Thumbnails.MICRO_KIND for 96 x 96
Thumbnails.MINI_KIND for 512 x 384 px
use this code
String filePath = "/sdcard/DCIM/Camera/my_video.mp4"; //change the location of your file!
ImageView imageview_mini = (ImageView)findViewById(R.id.thumbnail_mini);
ImageView imageview_micro = (ImageView)findViewById(R.id.thumbnail_micro);
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);
imageview_mini.setImageBitmap(bmThumbnail);
Check this link

Why size of image is not same when i download an image from a url?

I am downloading an image from a url and create a bitmap image in my android app. Size of image is 126 kb. When i create an image in my app by downloading it from url then size is around 3.25 mb. I am using following code to downlad image from url:
URL urlConnection = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
Why downloaded size of image is bigger and is there any way to download it in the same size? Thanks in advance.
Image you download is probably in some compressed format (.jpg .png). Bitmap you get from BitmapFactory.decodeStream on the other hand is in uncompressed format.
If you dont need to show your bitmap in original resolution you can scale it down (detailed instructions here). Otherwise such memory usage is normal.
Android converts this image to bmp. You can convert it by youself and you get size ~3.5 mb.
Dont use Bitmapfactory for downloads. Use an InputStream and a FileOutputStream to save to internal, external or removable memory directly.
Image size depends on bits per pixel .during conversion you have to give bits per pixel
file size is given by Resolution^2 x Width x Height x Bits per sample ÷ 8,192

Android change image size before sending via HTTP [duplicate]

This question already has answers here:
Get the image as Thumbnail
(2 answers)
Closed 9 years ago.
My app is currently sending images from an Android device to a PHP script by converting the image into a bit array and then converting to base64. The base64 string is then sent in a HTTP request.
The problem is that is the image is big (like the ones taken from android camera) then the transfer fails. What i want to do is change the image size before it goes through the conversion process.
How can i do this? I've tried to google it but have had no luck so far.
If your image size is big then you have to need first scale in to small size then encode this by base64 class then you send this on your server.
For scale your image read this http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application
or other post
Use jpeg compression!
('Cause I'm not sure how sending up a base64 encoded byte array is going to save you any space.)
May I jump in and assume you've got a stage where you've converted your image into an array of pixels instead? If not, I'll assume there is no reason why the obvious conversion from bytes to integers representing pixels applies. Then we'll convert it to a compressed jpeg.
final int[] pixels = yourpixels;
You'll also need width and height:
final int width = theWidth; etc...
Next, get hold of your output stream in your client:
final HttpURLConnection connection = doWhateverYouDoToOpenYourConnection();
final OutputStream httpOutputStream = connection.getOutputStream();
Now the crucial step is to use the compression methods of Android's bitmap library to stream the compressed image onto the http output stream:
final Bitmap androidBitmap = Bitmap.createBitmap(pixels, width, height,Config.ARGB_8888);
androidBitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, YOUR_QUALITY_INT, outputStream);
Start with something like YOUR_QUALITY_INT = 85 to see significant improvement in image size without much visible deformation.
If this fails, create a scaled bitmap from a scale matrix: documentation here. This reduces the width and height of your bitmap on creation, which obviously reduces request size.
Hope this helps.

Categories

Resources