Android BitmapDecode in listview - android

I use the folowing to get a bitmap from my device and display it in a listview
pic1="Harris1.jpg"
pic2="Harris2.jpg"
pic3="Harris3.jpg"
Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic1);
Bitmap bmp1 = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic2);
Bitmap bmp2 = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic3);
Bitmap[] image={bmp,bmp1,bmp2};
for(int i=0;i<text1.length;i++)
{
item_details.setImage(image[i]);
}
this works fine but is there a way to buildup the Bitmap {} image without having to put a line for each = BitmapFactory.decodeFile?
I want to be able to read the filenames from a database which I can do but sometimes there are just 3 pictures but other times here may by 50 and I want to be able for the routine to do the {bmp,bmp1,bmp2 etc...} automatically
Any ideas?
Your help appreciated
Mark

Based on your code sample. Using the String.format method is the simplest way to achieve your goal.
String picFormat="Harris%d.jpg";
for(int i=0;i<text1.length;i++)
{
String pic = String.format(picFormat, i+1);
Bitmap bmp = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + pic);
item_details.setImage(bmp);
}

Related

load all images from internal memory to a viewPager android

i have designed an app to show images in a view Pager, now the thing is that user saves this images to its internal memory and the app gives a random no. to the image as name, upon clicking "View Favorite " button the user gets to view all the images in a view pager one by one, can any1 help me as to how i go about it??
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "image.png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
Context context =getApplicationContext();
final ImageView imageView = new ImageView(getApplicationContext());
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
this is the code to load the image but the thing is i need to load all the images not just one image.png but everything.png ;) how do i do it??
and also i wanna load with Picasso but i cant load the bitmap it says something like load is not for bitmap etc. etc. please help
I don't know if you have already solved this issue. But if you don't give your files a random number when you save them, you could give them sequential numbers like 1.jpg, 2.jpg .... then you could do a while loop to load each image. For example:
int counter = 0;
boolean imageExists = true;
while(imageExists)
{
File imageFile = new File (filePath + counter + ".jpg");
if(imageFile.exists())
{
Picasso.with(getBaseContext()).load(imgFile).fit().centerInside().into(imageView);
}
else
{
imageExists = false;
}
}
Hopefully this helps.

The most efficient way to show the local image in android

I am currently work on a magazine like apps. Since there is an option to zoom in(150%, 200%, 250% of original source) , I would prefer not to scale down the image. However , the app will force restart when I try to decode the image because of the out of memory. Are there any suggestion to fix that?
The image are local (SD card) , can be retrieve in any approach, but eventually need to be a bitmap as I use something like cavans.drawbitmap to display it. I tried, input stream-> bytes , input stream->bitmap etc... but are there any most efficient way or at least
I can sure the app does not force restart / close? Thanks
try {
InputStream is = (InputStream) new URL(url).getContent();
try {
return BitmapFactory.decodeStream(is);
} finally {
is.close();
}
} catch (Exception e) {
return BitmapFactory.decodeResource(context.getResources(), defaultDrawable);
}
You should consider using nostra's image loader, it deals with memory quite efficiently, you can specify lots of config stuffs, im using it and its working pretty well for large images aswell
This is what smartImageView(by loopj - you can find him on http://loopj.com/) uses to retrieve files from the drive/sd.
private Bitmap getBitmapFromDisk(String imgID) {
Bitmap bitmap = null;
if(diskCacheEnabled){
String filePath = getFilePath(imgID);
File file = new File(filePath);
if(file.exists()) {
bitmap = BitmapFactory.decodeFile(filePath);
}
}
return bitmap;
}

problems with big drawable jpg image

during a couple of days i've been looking here and on the Internet in many post for a solution on an issue im having loading or moving a large jpg image (located as a drawable reource by now) and I think it's time to ask myself altought there are many questions and solutions very related.
I'm developing a 3D terrain simulation with OpenGL and the problem is that I'm using as terrain texture a quite large jpg image ( 2048x2048 = 1,94 MB ).
Let's look at some workarround I Did:
1st:
Bitmap terrainBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.terrain, options);
This one crashes inmediately, probably because the image is too big
2nd:
Uri path = Uri.parse("android.resource://com.jgc.my_app/" + R.drawable.terrain);
URL ulrn;ulrn = new URL(path.getPath());
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
that one probably was a quite silly attempt
3rd:
Bitmap terrainBitmap = BitmapFactory.decodeFile("android.resource://com.jgc.my_app/terrain.jpg", options);
but I'm getting a very frustrating null bitmap as result...
Other workarrounds in mind are, as the drawable resources are "inside" the application, why not to move them to the sd card and decodefile form there, but, the solutions given to move a bitmap or a drawable to the sdcard that I've found always user BitmapFactory.decodeResource(getResources(), R.drawable.terrain, options);
I appreciate any suggestion, since then I'll be working arround other tasks in my proyect.
Thanks in advance
Ok, maybe I've been lucky, here is the n work-arround ind this workd for me:
private static BitmapFactory.Options terrainBitmapOptions = new BitmapFactory.Options();
...
// Set our bitmaps to 16-bit, 565 format.
terrainBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
...
InputStream is = getResources().openRawResource(R.drawable.terrain);
Bitmap terrainBitmap;
try {
terrainBitmap = BitmapFactory.decodeStream(is, null, terrainBitmapOptions);
} finally {
try {
is.close();
} catch (IOException e) {
// Ignore.
}
}
I hope it helps anyone else...

How to find origID for getThumbnail when taking a picture with camera.TakePicture

For getThumbnail, the android documentation has:
public static Bitmap getThumbnail (ContentResolver cr, long origId, long groupId, int kind, BitmapFactory.Options options)
I have absolutely no idea how to get origId (The ID of the original image to perform getThumbnail on) when taking a picture with Camera.TakePicture.
My current attempt, based on various other questions I've read is:
String[] projection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA };
String sort = MediaStore.Images.ImageColumns._ID + " DESC";
Log.d("getting IDs:",sort);
Cursor myCursor = managedQuery(imagesUri, projection, null, null, sort);
myCursor.moveToFirst();
thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID)), MediaStore.Images.Thumbnails.MINI_KIND, null );
However, my log is outputting the string "_ID" for what should be the actual ID, and it then gives me a null pointer exception on the line where I try and create myCursor.
I also read as the answer to somebody else's similar question that images on the SD card don't have IDs, in which case I guess origID would actually be a URI and the docs are just messed up? I am extremely confused, and any explanation would be very very welcome.
I ended up not being able to use getThumbnail, as I could not find any working way to use the path to the location of the image succsessfully, and (at the time at least, I believe there have been reports submitted) it had issues with devices not storing their thumbnails in the expected location.
My solution to this ended up being what I had hoped I could avoid, writing my own little thumbnail generator instead of using Android's getThumbnail.
public class CreateThumbnail extends Activity {
Bitmap imageBitmap;
public Bitmap notTheBestThumbnail(String file) {
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 95;
FileInputStream fis = new FileInputStream(file); //file is the path to the image-to-be-thumbnailed.
imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos); //What image format and level of compression to use.
imageData = baos.toByteArray();
}
catch(Exception ex) {
Log.e("Something did not work", "True");
}
return imageBitmap;
}
}
I use the class like:
CreateThumbnail thumb = new CreateThumbnail();
thumb.notTheBestThumbnail(Environment.getExternalStorageDirectory() + "/exampleDir" + "/" + exampleVar + "/example_img.jpg");
Bitmap mBitmap = thumb.imageBitmap; //Assigns the thumbnail to a bitmap variable, for manipulation.
While I didn't actually figure out how to get the ID, hopefully this will help anybody facing similar problems with getThumbnail.

Extra space above and below Android Bitmap

I am downloading a bitmap from a website and then displaying it in my application. Whenever I download this image and set it into an ImageView, there is always a lot of extra space above or below the actual image. This extra space is part of the ImageView and is only there after I set the ImageBitmap to the downloaded bitmap.
So, this is making me think that the extra space is somehow part of the bitmap.
However, when I download the same image in a Webview, there is no extra space.
If you have any ideas on why this could be happening, please let me know! Let me know if you need any more information, thanks.
Edit: Here's my code getting the bitmap:
InputStream in = null;
Message msg = Message.obtain();
msg.what = 1;
try{
in = openHttpConnection(_url);
if (in != null)
{
Bitmap bit = BitmapFactory.decodeStream(in);
Bundle b = new Bundle();
b.putParcelable("bitmap", bit);
msg.setData(b);
in.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
_handle.sendMessage(msg);
And this is what I use to then for the ImageView, I get the bitmap from the code above and:
imageV.setImageBitmap(comic);
Edit 2:
After trying this with some other images from different website, I've found that this white space is not always there. Given that, and there's probably not anything wrong with the code, are there any suggestions on removing this extra space since it doesn't show up in the actual image online nor in a webview?
imageSize = din.readInt();
imageName = din.readUTF();
byte b[] = new byte[imageSize];
din.readFully(b);
bmImg = BitmapFactory.decodeByteArray(b,0,b.length);
//This works for me....
//din is DataInputStream object.

Categories

Resources