Reputation: 191
Need to get image from path. I have tried everything but don't seem to get the image.
My two image paths:
/storage/emulated/0/DCIM/Camera/20161025_081413.jpg
content://media/external/images/media/4828
How do i set my image from these paths?
I am using ImageView to display my image.
My code:
File imgFile = new File("/storage/emulated/0/DCIM/Camera/20161025_081413.jpg");
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.myimage.setImageBitmap(myBitmap);
Thanks in advance
Upvotes: 1
Views: 661
Reputation: 191
I found my problem. I am using Android SDK 23.
From the Android documentation.
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
https://developer.android.com/training/permissions/requesting.html
Hopes this helps someone else
Upvotes: 0
Reputation: 1505
Regularly, you can just write BitmapFactory.decodeBitmap(....) etc, but the file can be huge and you can get the OutOfMemoryError in no time, especially, if you do decoding a few times in the row. So you need to compress the image before setting it to view, so you won't run out of memory. Here is the proper way to do it.
File f = new File(path);
if(file.exists()){
Bitmap myBitmap = ImageHelper.getCompressedBitmap(photoView.getMaxWidth(), photoView.getMaxHeight(), f);
photoView.setImageBitmap(myBitmap);
}
//////////////
/**
* Compresses the file to make a bitmap of size, passed in arguments
* @param width width you want your bitmap to have
* @param height hight you want your bitmap to have.
* @param f file with image
* @return bitmap object of sizes, passed in arguments
*/
public static Bitmap getCompressedBitmap(int width, int height, File f) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(f.getAbsolutePath(), options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(f.getAbsolutePath(), options);
}
/////////////////
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Upvotes: 1