How i set image in code editor extension

howto

#1

I wrote this code:

public void Set(Image img,String imgname){
ImageView iv=(ImageView)img.getView();
iv.setImageDrawable(imgname);
}

result error:
incompatible types: String cannot be converted to Drawable
Thanks in advance for helping.


#4

If you want to use .setImageDrawable then you could use something like this:

Drawable drawable = Drawable.createFromPath(imgname);
iv.setImageDrawable(drawable);

#5

Drawable.createFromPath works correctly in development mode with the path “/ mnt / sdcard / AppInventor / assets /”, but when I build the apk the path
“/ android_asset /” result is nul,
no image is loaded.
I thinked problem was undescore and changed it with “% 5f”, but the result is the same.
Here is the code:

@SimpleFunction (description = “Set Value”)
public void Set (Image img, String imgname, boolean development, Label label, String title) {
TextView tvv = (TextView) label.getView ();
ImageView iv = (ImageView) img.getView ();
String pathimg = “”;

if (development == true) {
pathimg = “/ mnt / sdcard / AppInventor / assets /” + imgname;
}
else {
pathimg = “/ android_asset /” + imgname;
}

   tvv.setText (pathimg);
   Drawable drawable = Drawable.createFromPath (pathimg);
   iv.setImageDrawable (drawable);
   }




any suggestions?

<a class="attachment" href="/uploads/default/original/2X/7/72e11fe51a667eea378beaf225edb4863b111a31.aia">img.aia</a> (52.0 KB)

#6

Try this:

Drawable drawable = null;
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(imgname);
            drawable = Drawable.createFromStream(inputStream, null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

#7

Solved:
here is the code:

@SimpleFunction(description = “Set Value”)
public void Set(Image img,String imgname,boolean development,Label label,String title) {
TextView tvv = (TextView)label.getView();
ImageView iv=(ImageView)img.getView();
String pathimg ="";

if(development== true){
pathimg = “/mnt/sdcard/AppInventor/assets/”+imgname;
}
else{
pathimg ="/android_asset/"+imgname;
}

  tvv.setText(pathimg);
 // Drawable drawable = Drawable.createFromPath(pathimg);
 // Drawable drawable = Drawable.createFromPath("/android_asset/london.jpg");
  //iv.setImageDrawable(drawable);
  Drawable drawable;
try {
  drawable = MediaUtil.getBitmapDrawable(container.$form(), imgname);
} catch (IOException ioe) {
  Log.e("Image", "Unable to load " + imgname);
  drawable = null;
}

ViewUtil.setImage(iv, drawable);

  }

Thanks a lot Ken for your effort.
Gretings from Italy.