4

I already have some pictures on my drawable folder from android project.

I create some objects (Agents) and then I need to set the imageView with this picture I saved on database.

So, I am saving the picture as String photoPath:

Uri path1 = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.agent1);
        String photoAg1 = path1.getPath();
ag1.setPhotoPath(photoAg1);

(I have already tried path1.toString.)

Ok, no problems until there. This object is shown at a listView with ImageView.

To show it there, I am doing:

    ImageView photo = (ImageView) view.findViewById(R.id.list_cell_photo);
System.out.println(agent.getPhotoPath());                    
Bitmap bitmap = BitmapFactory.decodeFile(agent.getPhotoPath());
                    Bitmap lowdefbitmap = Bitmap.createScaledBitmap(bitmap,300,300,true);

                    photo.setImageBitmap(lowdefbitmap);

The problem is on the createScaledBitmap. Error above:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
                                                                   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:714)

Return from the System.out.println: /2131099732 If I put path1.toString the return is: android.resource://com.aa.xlambton/2131099732

Ive already took a look at: Create a Bitmap/Drawable from file path

Android get image path from drawable as string

Get absolute path of android drawable image

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException even when file IS actually there

I think I am saving this path wrong, because Bitmap cannot decode the file, so it is going empty to createdScaledBitmap.

Can someone help me please?

asr
  • 139
  • 3
  • 11
  • do u want to convert drawable as bitmap? why do u need it? just simply set the drawable as image resource – Jyoti JK Dec 13 '17 at 08:12
  • Possible duplicate of this [link](https://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap) – Jyoti JK Dec 13 '17 at 08:15

4 Answers4

1

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);
islamdidarmd
  • 725
  • 5
  • 19
0

You can try to use this method:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.youResourceId);
alex samsung
  • 110
  • 1
  • 10
0

try like this

public int getImage(String imageName) {

    int drawableResourceId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());

    return drawableResourceId;
}
Benjith Mathew
  • 1,211
  • 1
  • 11
  • 23
0

In my project, I want to get some images from project folder. So there is a way to get your images in java class, however it is simple in .xml file. it is clearly done by assets Manger in android. Follow this link
Notice: AssetManager assetManager = getAssets(); must be in youractvity.java file.