Create a Bitmap/Drawable from file path
Create bitmap from file path:
File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
If you want to scale the bitmap to the parent's height and width then use Bitmap.createScaledBitmap
function.
I think you are giving the wrong file path. :) Hope this helps.
Android get image to Bitmap from filepath
Use This Method to get Bitmap from Filepath
public Bitmap getBitmap(String path) {
Bitmap bitmap=null;
try {
File f= new File(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap ;
}
Getting bitmap from file path
You are not incrementing imageCounter
, so it actually is an endless loop.
An enhanced for loop is less error prone:
for (String uri : imageUris) {
...
Bitmap bitmap = BitmapFactory.decodeFile(uri);
...
How to get path from image on drawable folder and set as image view, Bitmap?
You can save Reference Id R.drawable.img_name
as integer rather than saving path.
When you need to manipulate this drawable you can use that id instead of R.drawable.img_name
.
If you need a bitmap from this drawable follow this
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
saved_id);
creating BitmapDrawable from path
Ok... I think I have a handle on your problem now. Like I said, you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically, declare a HashMap somewhere in your class:
private static HashMap<String, Integer> images = null;
Then initialize it in your constructor:
public myClass() {
if (images == null) {
images = new HashMap<String, Integer>();
images.put("Human1Arm", R.drawable.human_one_arm);
// for all your images - don't worry, this is really fast and will only happen once
}
}
Now for access -
String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);
Convert a File Object to Bitmap
You should be able to use BitmapFactory
:
File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
How to convert a Drawable to a Bitmap?
This converts a BitmapDrawable to a Bitmap.
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
Related Topics
How to Remove All Callbacks from a Handler
Hosting an Executable Within Android Application
Full Screen Videoview Without Stretching the Video
How to Select and Crop an Image in Android
How to Skip Initial Data and Trigger Only New Updates in Firestore Firebase
How to Set Up Intellij Idea for Android Applications
How to Use Weakreference in Java and Android Development
Reading a JSON Array in Android
String.Equals() with Multiple Conditions (And One Action on Result)
Android Timer Updating a Textview (Ui)
Best Way to Implement View.Onclicklistener in Android
Java - Convert String to Valid Uri Object
Create Pdu for Android That Works with Smsmessage.Createfrompdu() (Gsm 3Gpp)