Bitmap in ListView: Loading slowly - android

In my ListView, I have one ImageView and two TextViews in every row. The pictures for the ListView are loaded from the memory of my Galaxy Nexus. I already downscaled it to 100x100 using
Bitmap scaled = Bitmap.createScaledBitmap(de, 80, 80, true);
but it still needs a few seconds to load.
What can I do?
EDIT:
public Bitmap resizeBitmap(String path){
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream is = null;
try {
is = new FileInputStream(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BitmapFactory.decodeStream(is,null,options);
try {
is.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
is = new FileInputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// here w and h are the desired width and height
options.inSampleSize = Math.max(options.outWidth/100, options.outHeight/100);
// bitmap is the resized bitmap
Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
return bitmap;
}

First point i can see from your code that you are decoding the stream twice...the first place where it is decoded its not being even used anywhere....So removing that statement will increase the execution speed of your code.
//BitmapFactory.decodeStream(is,null,options); comment this line
try {
is.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Also may I ask why can you not use the Thumbnails of the pictures? instead of resizing every image....if you are trying to display thumbnails of images in your app
Yes there is a whole table that stores the thumbanil of images you need to access it via Cursor api for example:
Cursor mediaCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
null, null);
From this cursor you can get the imageId and then with the help of image Id you can retreive the thumbnail of the image by using the MediaStore.Images.Thumbnails.getThumbnail() method for example below is some code that will help:
Bitmap thumbBitmap = null;
thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageID, MediaStore.Images.Thumbnails.MICRO_KIND, null);
if(thumbBitmap != null){
return new BitmapDrawable(thumbBitmap);
}

Displaying bitmaps in ListView efficiently is a fairly complex task that involves quite a lot of variables. I would suggest you reading this and downloading a code example they provide. This is an excellent starting point and in simple cases may be enough to be copy-pasted.

The best way is to load them in AsyncTask. Here you have a great video that shows this approach:
https://developers.google.com/events/io/2012/sessions/gooio2012/103/

Related

how to get RGB values from byte[] array in android camera2 when ImageFormat is JPEG?

I'm using camera2 API I'm interested in getting RGB value from byte array actually average of RGB values can I get it from this part when byte array is collected from ByteBuffer and gets written in file with FileOutputStream.write(bytes)
Here is my code:
ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(mImageFile);
fileOutputStream.write(bytes);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); } catch
(IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
finally {
mImage.close();
try {
fileOutputStream.close();
}
catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
The Image class reference page implies that this BitmapFactory method might be what you're looking for, to convert the JPEG format data to a Bitmap.
However, if you don't need the data to be in JPEG format for saving to a file as well (i.e. if you're simply processing images from the camera in the app), you may want to consider changing the output Surface format from JPEG to YUV_420_888. Then you can convert that to RGB Bitmap (see this answer) and save the considerable amount of time it takes to encode JPEG.

How to put a image in a pdf using itext library in android?

I have a png image in drawable folder and I want to use that png image in the pdf . I am using itextpdf.jar for creating the pdf. I searched for solution ,but I am not able to figure it out.
try {
InputStream inputStream = MainActivity.this.getAssets().open(
"launchscreen.png");
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image signature;
signature = Image.getInstance(stream.toByteArray());
signature.setAbsolutePosition(400f, 150f);
signature.scalePercent(100f);
document.add(signature);
document.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I hope its useful to you.
You can use
Image image = Image.getInstance(yourImageByteArray);
document.add(image);
where document is an object of Document

How to fetch images from gallery and move in android application

I am working on Android Security app and I have to lock the images from gallery so I am trying to move images from gallery into my application.
I am using this code:
Cursor cursor = cursor1[0];
int i = 0;
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));
long idC = Long.parseLong(id);
Uri newUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, idC);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr,newUri);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,stream );
Bitmap compBit = Bitmap.createScaledBitmap(bitmap, 100,100, true);
bitmap1[i] = compBit;
bitmap.recycle();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
but getting this exception:
Out of memory on a 4192360-byte allocation.
there is a training module on Displaying Bitmaps Efficiently - [link] http://developer.android.com/training/displaying-bitmaps/index.html .
It provide good information on handling out of memory exception with sample.

How to take a screenshot of activity(game) programmatically

I am aware that not all Android devices can take screen shots, but I would like to be able to take screen shots of my users game screen programmatically so they can share it with friends etc. Kinda like Hill Climb racing does in there game.
Does anyone know how to do this? I tried a few solutions that are posted on here:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = activity.getCurrentFocus().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But no luck. Any other way to do this? Maybe a new method.
EDIT:
I am getting a null pointer at line View v1 = activity.getCurrentFocus().getRootView();
Try This In this I take screen shot of current layout and save in sdcard...
To Capture Screen Shoot
Use
View v1 = getWindow().getDecorView().getRootView();
in place of
View v1 = activity.getCurrentFocus().getRootView();
it will not throws Null Pointer Exception.
Enjoy!!

Loading images from cache but return a blank screen

i try the example i gotten from here. The example shows how to display a image that is larger then the screen and the screen acting like a window, allowing user to look at the images via scrolling. However, what i want to achieved is similar just that, instead of one single image, i tried to combining 18 smaller images (171X205) into one single image. I am able to did that but to save loading time on downloading of images from the server, i cached the images. Heres the problem, i cant seems to display the images out on screen even though the images are indeed cached. Thus, i tried something else by fetching image from the drawable folder but still the same issue arise. Does anyone have any idea how to go about solving this problem?
A snippets of code to load images from cache:
for(int i =0; i<18; i++)
File cacheMap = new File(context.getCacheDir(), smallMapImageNames.get(i).toString());
if(cacheMap.exists()){
//retrieved from cached
try {
FileInputStream fis = new FileInputStream(cacheMap);
Bitmap bitmap = BitmapFactory.decodeStream(fis)
puzzle.add(bitmap);
}catch(...){}
}else{
Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString());
if(i==0){
height1 = smallMap.getIntrinsicHeight();
width1 = smallMap.getIntrinsicWidth();
}
if (smallMap instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap();
FileOutputStream fos = null;
try {
cacheMap.createNewFile();
fos = new FileOutputStream(cacheMap);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
puzzle.add(bitmap);
}
}
}
The function where it retrieved images from the server
private Drawable LoadImageFromWebOperations(String url) {
// TODO Auto-generated method stub
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
I am drawing onto a canvas and so no ImageView is use to display the images.
For all of my lazy image loading I use Prime.

Categories

Resources