Herewith I need to load an Image Button dynamically with locally saved .png file. From internal storage of the device I collect the images found on particular folder. Its working fine.
List<String> fileNames = new ArrayList<>();
File folder = new File(Environment.getExternalStorageDirectory(), "Pictures/Screenshots");
if (!folder.exists()) folder.mkdir();
for (File file : folder.listFiles()) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".jpg") || filename.endsWith("jpeg") || filename.endsWith(".png")) {
fileNames.add(filename);
}
}
log results as
[123.png]
finally I need to set the background as 123.png image for the image button. To do that I have used inside the loop
String picName = fileNames.get(i);
String picName1 = picName.replace(".png", "");
int resID = getResources().getIdentifier(picName1,"drawable","com.test.ABC");
imageView.setImageResource(resID);
At that moment I got this error
11-21 17:54:48.899 27250-27250/com.datamation.swdsfa W/ResourceType: No package identifier when getting value for resource number 0x0000007b 11-21 17:54:48.904 27250-27250/com.datamation.swdsfa W/ImageView: Unable to find resource: 123 android.content.res.Resources$NotFoundException: Resource ID #0x7b at android.content.res.Resources.getValue(Resources.java:2350) at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330) at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:433) at android.support.v7.widget.VectorEnabledTintResources.getDrawable(VectorEnabledTintResources.java:67) at android.widget.ImageView.resolveUri(ImageView.java:648) at android.widget.ImageView.setImageResource(ImageView.java:377) at com.test.ABC.fragment.FragmentTools.ViewImageList(FragmentTools.java:342) at com.test.ABC.fragment.FragmentTools.onClick(FragmentTools.java:287) at android.view.View.performClick(View.java:4640) at android.view.View$PerformClick.run(View.java:19421) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)
Thanks in advance.
The method which you are using imageView.setImageResource(resID);
is for images present in drawable
folders, but as you are saying From internal storage of the device I collect the images found on particular folder.
seems like you are trying to use image stored in some directory in internal storage. So it won't work.
You can try something like this,
File file = new File("some path");
if(file.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
To get path of default camera directory you can try like,
File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(picDir, "your image.png");
Hi you can simply do this with Picasso or Glide
I'm giving the example to use Picasso.
File file = new File(filepath);
Picasso.with(activity).load(file).fit().centerCrop().into(image);
you need change little bit code
List<String> fileNames = new ArrayList<>();
File folder = new File(Environment.getExternalStorageDirectory(), "Pictures/Screenshots");
if (!folder.exists()) folder.mkdir();
for (File file : folder.listFiles()) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".jpg") || filename.endsWith("jpeg") || filename.endsWith(".png")) {
fileNames.add(Environment.getExternalStorageDirectory() + "Pictures/Screenshots/" +filename);
}
}
And you can use simply Picasso library to shoe the image into Image-Button
For Example
Picasso.get().load(filename).into(yourImageButton);
for that you need to add library into gradle
implementation 'com.squareup.picasso:picasso:2.5.2'
For reference, you can use this link Picasso
User contributions licensed under CC BY-SA 3.0