0

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.

3 Answers 3

1

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.

Check this

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");
Sign up to request clarification or add additional context in comments.

4 Comments

Thank bro. but Still not working for this path "DCIM/Camera/20181221_125634.jpg" without any errors
Got it. but if (file.exists()) inside not fired
/storage/emulated/0/DCIM
Thankx bro. Now I got an outofmemory error due to loop. But fired inside if condition. Now I'll fix it. Thankz again bro.... you saved my time
0

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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.