How can i create block where developer can input a image from assets?

yes i need it as @AryanGupta said

oh yeah :smiley:

1 Like

yes broo, Sur @Srrazmi is Correct

is there any more good code like @Mohamed_Tamer

I’m confused why @Mohamed_Tamer code doesn’t work :pensive:

1 Like
   @SimpleFunction(description = "")
    public void pick(AndroidViewComponent layout, String imagePath) throws Exception {
      InputStream in;
       try{
           in = container.$form().openAssetForExtension(imagePath);
       } catch (IOException e) {
} finally {
              in.close();
           }
          Bitmap bmp = BitmapFactory.decodeStream(in);
    Image img = (Image) bmp;
    setImage(img);
                }

    @SimpleEvent
    public void setImage(Image img) {
    //dispatch the event
    }

use this code. i just add IOException.

1 Like

@DevYB sur, your code is not working. it store nothing in in InputStream.

it gives me a error - in.close on a null object reference

This may help you to directly get image(bitmap) from any path or asset
MediaUtil.getBitmapDrawable(form, imageURI).getBitmap();

3 Likes

thanks , bery much @Atom_Developer

yeah I think using MediaUtil is a better way

1 Like

@Atom_Developer what’s wrong with my code?

@SimpleFunction(description = "")
   public void pick(AndroidViewComponent layout, String imageURI) {
     Drawable drawable = MediaUtil.getBitmapDrawable(container.$form(), imageURI).getBitmap();
     View view = layout.getView();
     view.setImageDrawable(drawable);
   } 

Error :

AndroidRuntime:
    [javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/salmanappinventor/Testaix/Testaix.java:55: error: incompatible types: Bitmap cannot be converted to Drawable
    [javac]      Drawable drawable = MediaUtil.getBitmapDrawable(container.$form(), imageURI).getBitmap();
    [javac]                                                                                            ^
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/salmanappinventor/Testaix/Testaix.java:57: error: cannot find symbol
    [javac]      view.setImageDrawable(drawable);
    [javac]          ^
    [javac]   symbol:   method setImageDrawable(Drawable)
    [javac]   location: variable view of type View
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 2 errors
    [javac] 1 warning

i think the code , provided by @Atom_Developer is returning a bitmap

so you cannot use this .

Drawable drawable =MediaUtil.getBitmapDrawable(container.$form(), imageURI).getBitmap();

use this something like that

Bitmap drawable =MediaUtil.getBitmapDrawable(container.$form(), imageURI).getBitmap();

1 Like

Code To Get Drawable
Drawable drawable =MediaUtil.getBitmapDrawable(container.$form(), imageURI);

2 Likes

actually I love drawable more than Bitmap :sweat_smile:

btw, you can convert the bitmap to drawable. using this method

Drawable drawable = new BitmapDrawable(getResources(), bitmap);

but it is easy to use @Atom_Developer code. Go for it

2 Likes

@Salman_Dev What Do You Exactly Want To Do Otherwise This Game May Be Played To A Month Or Year

1 Like

What are you talking about :astonished:

I Means What Do You Exactly Want To Do ?

Q1. Do You Want User Input Name Of Assets Image And Show It In Given Arrangement As A Background ?

Q2. Do You Want User Input Name Of Assets Image And Show It In Given Arrangement With Image ?

Q3. Or Do You Just Want To Pass The Name Of Assets And User Can Set It In ImageView Component That’s In Build

1 Like

I Want User Input Name Of Assets Image And Show It In all type components