imageView.setImageBitmap(bitmap) allways null while bitmap not null - android

i pass a String photo path to another activity, then i convert it to Uri (because photo path was converted from Uri), then i made the inputstream by uri, and made bitmap by that inputstream. bitmap is created and not null, but when i call imageView.setImagebitmap(bitmap), system give error that:
void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference.
private void showImage(Uri mPath) {
PhotoPath=mPath.toString();
InputStream is = null;
try {
is = getContentResolver().openInputStream(mPath);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
photo.setImageURI(mPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
what problem i met, please help me ! thank you

It sounds like your ImageView itself is null. Resolving that problem will probably fix your error.

Related

Bitmap cannot be created from an image file

As part of my program, I need to show images belonging to a folder (JPG files). To accomplish that, I have this code:
private ArrayList<ImageGalleryItem> getData() {
final ArrayList<ImageGalleryItem> imageItems = new ArrayList<>();
try {
Intent intent = getIntent();
String imagesFolder = intent.getStringExtra("SourceFolder");
String questionId = Integer.toString(intent.getIntExtra("QuestionId", 0));
File file = new File(imagesFolder);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
try {
if (f.getName().startsWith(questionId + "_")) {
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
imageItems.add(new ImageGalleryItem(bitmap, f.getName()));
}
}
catch (Exception e)
{
Log.e("TDC#", e.getMessage());
}
}
}
}
catch (Exception e)
{
Log.e("TDC#", e.getMessage());
}
return imageItems;
}
The instruction "BitmapFactory.decodeFile(f.getPath());" creates the bitmap files for all files but for one of them. When that code is reached for the conflicting file, decodeFile throws an exception, but the stranger thing is that the exception is not catched by the try catch block.
When debugging using F7, the exception is thrown in BitmapFactory.java, in the throw shown in this code:
try {
bm = nativeDecodeByteArray(data, offset, length, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
So the questions are, why the bitmap cannot be correctly decoded and why the exception is not catched.
If I browse for the file in mobile file manager, and open the file, it is shown correctly, so, there is no problem with the image format. Furthermore, this image was taken with the camera, the same as the other images that do decode correctly.
How to solve this problem or, is there an alternate way to do this?
I don't know why that happens. Try this:
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));

crop image save in internal storage and get bitmap from uri

I write a code crop image.The image maybe a little big,so I use intent.putExtra("return-data", false); and intent.putExtra("output", outputUri); So I should decode the uri to get bitmap.Just like this(omActivityResult)
InputStream is = null;
is = getContentResolver().openInputStream(outputUri);
Bitmap avatar = BitmapFactory.decodeStream(is);
I first make External Storage to store the crop image.It work well.
outputUri = Uri.fromFile(new File("/storage/emulated/0/upload.jpg"));
But I need to store it in Internal Storage.I follow the Android developer
to save in Internal Storage.The code follow.
FileOutputStream os = null;
try
{
os = openFileOutput("upload.jpg", 0);
}
catch (FileNotFoundException e)
{
}
finally
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e)
{
}
}
}
outputUri = Uri.fromFile(new File(getFilesDir().getAbsolutePath(), "upload.jpg"));
the avatar will be null.I have read many questions in stackoverflow but it doesn't work. Can you help me?

Get image rotation using filedescriptor and parcelfiledescriptor in android from gallery

I want to get an image with a device higher then android kitkat. Currently I use the filedescriptor and parcelfiledescriptor to get the image. I get the image succesfully, but rotated. As the image is stored on google drive I can't use the inputstream directly. How can I solve this? (I know image data is usually stored in exif but if I try to get exif data the rotation is always 0) I've also tried using the lib: https://github.com/coomar2841/image-chooser-library but I get the same issue here.
ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = activity.getContentResolver().openFileDescriptor(selectedImage, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
image = ExifUtil.rotateBitmap(selectedImagePath, image);
parcelFileDescriptor.close();
if (cropCircle) {
imageView.setImageBitmap(ImageUtils.cutCircle(image));
} else {
imageView.setImageBitmap(image);
}
return image;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Bitmap in ListView: Loading slowly

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/

Android : cannot read image in assets folder and assign to imageview

I have a file food.jpg in assets folder. I have try many ways. but I still cannot get this image into imageView. when run, instead of display image "food.jpg", it displays image that I have declared in xml file. (that image is in drawable folder).
The first way is:
int asset_id = context.getResources().getIdentifier("food", "drawable", context.getPackageName());
imageView.setImageResource(asset_id);
The second way is:
AssetManager assetManager = context.getAssets();
InputStream istr;
try {
istr = assetManager.open("food");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Please teach me how to fix this problem.
thanks :)
Your code is working fine. just add image MIME-Type(.jpg,jpeg,.png... etc) in below line:
istr = assetManager.open("ceo.jpg");
full code for your refrance
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open("ceo.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);
imageView.setImageBitmap(bitmap);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
Try this Code...
try {
// get input stream
InputStream ims = getAssets().open("food.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imgNews.setImageDrawable(d);
} catch (Exception ex) {
return;
}
}

Categories

Resources