Reading an image file into bitmap from sdcard, why am I getting a NullPointerException?
The MediaStore API is probably throwing away the alpha channel (i.e. decoding to RGB565). If you have a file path, just use BitmapFactory directly, but tell it to use a format that preserves alpha:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
or
http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html
Getting nullpointerexception for bitmap.getWidth() when reading image from sdcard
First just check whether your image in your required directory /sdcard/yourdollar/img001.jpg
And, Just try like this -
String myJpgPath = "/sdcard/yourdollar/img001.jpg";
Bitmap bm = BitmapFactory.decodeFile(myJpgPath);
and get image height & width in your Logcat
Log.i("width and height: ", bm.getWidth() + " " + bm.getHeight());
NullPointerException when convert and save Image file on Android device
It turns out that the image file has to be re-organized and as for Depth16 file, every pixel has 16 bits of data, so the code to convert it into a jpg file is:
Image depthimage = fromFrame.getImage();
int imwidth = depthImage.getWidth();
int imheight = depthImage.getHeight();
Image.Plane plane = depthImage.getPlanes()[0];
ShortBuffer shortDepthBuffer = plane.getBuffer().asShortBuffer();
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "depthImage.jpg");
Bitmap disBitmap = Bitmap.createBitmap(imwidth, imheight, Bitmap.Config.RGB_565);
for (int i = 0; i < imheight; i++) {
for (int j = 0; j < imwidth; j++) {
int index = (i * imwidth + j) ;
shortDepthBuffer.position(index);
short depthSample = shortDepthBuffer.get();
short depthRange = (short) (depthSample & 0x1FFF);
byte value = (byte) depthRange ;
disBitmap.setPixel(j, i, Color.rgb(value, value, value));
}
}
Matrix matrix = new Matrix();
matrix.setRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(disBitmap, 0, 0, imwidth, imheight, matrix, true);
try {
FileOutputStream out = new FileOutputStream(file);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MainActivity.num++;
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
I also rotate the picture to make it easier to review on mobile devices
java.lang.NullPointerException: uriString when receiving image sd card path
To use the path correctly you should first create it to have somewhere to store it, you can use that and later on delete it or use the data from the result you got.
Here is the code to create a Uri, pass it to your Intent. Once you get the result you can pass the Uri to another class by using getPath from the Uri package.
/**
* Creating file uri to store image/video
*/
public static Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
/**
* returning image / video
*/
private static File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"YOUR DIRECTORY NAME");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("image upload", "Oops! Failed create "
+ "YOUR DIRECTORY NAME" + " directory");
return null;
}
}
//TODO change naming
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
EDIT 1:
To convert a file to a bitmap you can use this code, provided by @Nikhilreddy Gujjula in this question
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
null pointer Exception when i downlaod image and store to Sd card in android
I think the Problem is here:
OutputStream output = new FileOutputStream(new File(
storagePath, "1.jpg"));
where your storagePath
is:
File storagePath = Environment
.getExternalStorageDirectory();
and later you try to Access the downloaded Image in:
String imagePath = Environment.getExternalStorageDirectory()
.toString() + "/LociiImages/" + "1" + ".jpg";
If you compare the storagePath and the path you are looking for, then you will see that you are searching in the sub-Folder LociiImages
but you didn't save your Picture there.
That's why this line:
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
throws a NPE. Just a Suggestion. Hope it helps.
Android: Reading a file from SD card and display on image view giving error
This statement:
ImageView myImage = (ImageView) findViewById(R.id.imgView);
cannot be done before the setContentView();
The way you've done it myImage
will be null, therefore the NullPointerException
I suppose you're getting.
Showing a photo from SD card to ImageView
Try this,
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
mImageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
also check your imageview mImageView
initialized or not
Related Topics
Do X86 Android Avd's Work on Linux on Amd
How to Change Endianness Mid-Execution on Arm (Android/Linux)
Android 5.0 Android:Elevation Works for View, But Not Button
How to Launch Termux Through Another Android App
Android Failed to Start Emulator:Cannot Run Program
How to Remove Strings from a Compiled Binary (.So)
How to Integrate Opencv into Qt Creator Android Project
Passing Data from Java Class to Web View HTML
Android Webview Err_Unknown_Url_Scheme
Media Queries Not Behaving as Expected on Android
Android - Multiple Screen Support Issue
Install Shows Error in Console: Install Failed Conflicting Provider
Getting Bitmap from Vector Drawable
How to Read Value from String.Xml in Android
Example Communicating with Handlerthread