First I save the image using FileOutPutStream etc..
This should be working since I am able to see the image in the SDCard using 'Astro File Manager'. (The path is also correct according to AFM)
However when I try to load this image as a Bitmap and display it in an ImageViewer I get nothing.
ImageView image = (ImageView) findViewById(R.id.IMAGE);
Bitmap bitmap = BitmapFactory.decodeFile(requestList.get(0).getImage());
image.setImageBitmap(bitmap);
I've checked the byteCount() on the bitmap and it returns a large number so I'm guessing that's fine.
So as I am able to load the file in AFM my guess is that it should be loadable by me as well, but maybe in another way that I'm currently attempting?
The Image is taken from another phones camera, uploaded to a server and then downloaded and stored by my phone. (I believe all of this works though)
Try this :
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
Related
I have a project on Android and iOS, there is a section where it shows a 360 view of a car, in iOS I just get the 60 images from internal storage that were previously downloaded from the internet, and these images are showed when the user starts to swipe the car to the right or to the left. And this process takes just 200ms to be ready to interact with the user.
But in Android I have to take first the 60 images and convert them in bitmaps, and then the process is similar to the iOS process. Nevertheless, this first process, convert all of them to bitmap takes about 3 seconds o more. I have read the process to show an image in Android in many sources of information and seems like is necessary use a bitmap for this task, so is possible to reduce the time to get all bitmaps?, or could I save a bitmap to avoid converting the PNG file again to a new bitmap when the users open the same view again?
The code that I'm using to convert png file to a bitmap object is the same that Android developers documentation recommend:
https://developer.android.com/topic/performance/graphics/load-bitmap
My source code for get bitmap is:
public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = 1;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bitmap =BitmapFactory.decodeFile(filePath, options);
Log.d("img360","bitmap res "+bitmap.getWidth()+ " h "+bitmap.getHeight());
return bitmap;
}
I'm really thankful for your help and support.
If you want to download images from Internet and display them on android
Just put an imageview and use picaso or glide library
It is faster and easier
I am facing this problem for 2 days now. I am making a 2D game via Canvas and it has a PNG file which I am drawing on the canvas by drawBitmap(), and it works fine.
But for Per-Pixel-Collision detection, I am using getPixel(), but it's always returning 0.
I am creating the bitmap in th following way-
Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
//image is a PNG file
but following always gives 0-
Toast.makeText(getContext(),""+bmp.getPixel(x,y), Toast.LENGTH_SHORT).show();
//x and y are within the boundary
I even tried the following, but same results-
BitmapFactory.Options opt=new BitmapFactory.Options();
opt.inMutable=true;
Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), R.drawable.image,opt);
Being a beginner,I don't have much understanding of bitmaps and color scheme, so, reason on why this is happening and any solution would be of great help.
My project is stalled midway for this problem.
Thank you
Got a solution on my own. Its rather a workaround, than a solution, but it works.
Just pass a BitmapFactory.Options object to BitmapFactory.decodeStream like the one below.
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeStream(context.getResources(), R.image,opt);
Drawable image = Drawable.createFromPath(newImagepath);
defective_image.setBackgroundDrawable(image);
The image is stored in the newImagePath variable which is a String. The above throws an outOfMemory Exception.
I also tried retrieving the image into a Bitmap object.
Bitmap bitmapImg= BitmapFactory.decodeFile(newImagepath);
In the above case the bitmapImg is Null. (Without any exception)
However if it is retrieved as a file, it's successful. This doesn't serve my purpose because I want this image to be the background of a RelativeLayout.
File imageFile = new File(newImagepath);
String imgPath = imageFile.getAbsolutePath();
The imgPath is the same as the newImagePath, concluding the path is not wrong. I also verified the image's existence in the SDCard.
This image was captured from the device's camera. This code is working on the emulator successfully. When debugged on the device the above said faults were noticed.
I also tried:-
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
It seems to be a memory issue. So I suggest to decode bitmap size with the option
options.inJustDecodeBounds = true;
then getting the size of this bitmap by looking at fields (don't try to use bitmap : it will be null) :
options.outWidth;
options.outHeight;
Then creating a scaled bitmap (depending on the screen dimension). To do this, you can look at
BitmapFactory.Options.inSampleSize
I would rather use a BitmapFactory to decode the Image from the file-path:
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
jpgView.setImageDrawable(bitmap);
I don’t want to open any gallery view. I know the name of an image e.g.: blabla.jpg, which is stored in memory card. I want to retrieve that specie image hardcoded and shown in an image view.
if you want to load image from SD card that you can try below code.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/blabla.jpg", options);
mImageView.setImageBitmap(bitmap);
Try:
ImageView.setImageUri(Uri.parse(new File("/sdcard/blabla.jpg").toString()));
In Android, how do you display an image (of any size) from the SD card, without getting an out of memory error?
Is it necessary to put the image in the Media Store first?
A pseudo-code example would be greatly appreciated. Extra points if the displayed image is as big as the memory level of the device allows.
Edit: this question has actually been already answered at Strange out of memory issue while loading an image to a Bitmap object (the two highest voted answers). It does also use the inSampleSize option, but with a small method to automatically get the appropriate value.
My original answer:
The inSampleSize of the BitmapFactory.Options class can solve your issue (http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize). It works by making a bitmap with the resulting width and height 1/inSampleSize than the original, thus reducing memory consumption (by inSampleSize^2?). You should read the doc before using it.
Example:
BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;
// don't ever use a path to /sdcard like this, but I'm sure you have a sane way to do that
// in this case nebulae.jpg is a 19MB 8000x3874px image
final Bitmap b = BitmapFactory.decodeFile("/sdcard/nebulae.jpg", options);
final ImageView iv = (ImageView)findViewById(R.id.image_id);
iv.setImageBitmap(b);
Log.d("ExampleImage", "decoded bitmap dimensions:" + b.getWidth() + "x" + b.getHeight()); // 1000x485
However here it will only work for images up to, I guess, inSampleSize^2 times the size of the allowed memory and will reduce the quality of small images.
The trick would be to find the appropriate inSampleSize.
I'm displaying any sized images using code:
ImageView imageView=new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
FileInputStream fis=new FileInputStream(file);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //try to decrease decoded image
options.inPurgeable=true; //if necessary purge pixels into disk
options.inScaled=true; //scale down image to actual device density
Bitmap bm=BitmapFactory.decodeStream(is, null, options);
imageView.setImageBitmap(bm);
fis.close();
For example:
yourImgView.setImageBitmap(BitmapFactory.decodeFile("/sdcard/1.jpg"));
http://www.developer.com/ws/other/article.php/3748281/Working-with-Images-in-Googles-Android.htm covers all you'll ever need to know on the subject of images, including getting them from an SD card. You'll notice the code to do so there, copied below:
try {
FileOutputStream fos = super.openFileOutput("output.jpg",
MODE_WORLD_READABLE);
mBitmap.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (Exception e) {
Log.e("MyLog", e.toString());
}