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

what are you trying to made ??
answer me right now

actually I am confused with that code :rofl:

I am interested, please tell the method

   @SimpleFunction(description = "")
    public void pick(AndroidViewComponent layout, String imagePath) {
      InputStream in;
       try{
           in = container.$form().openAssetForExtension(imagePath);
       } finally {
          in.close();
       }
      Bitmap bmp = BitmapFactory.decodeStream(in);
      
      View view = layout.getView();
Image img = (Image) bmp;
          view.img; 
        }

and please do not forget to use this import

import android.media.Image;

btw, is that code work for you

:woozy_face:

I don’t know how much java I have in my head :dizzy_face:

use this code

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

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

oh man, I think the code that you changed is wrong

1 Like

Keep in mind - that Bitmap is a subclass of Image. so, you need to cast it

does i am missing something

See you have to make it like this:

ImageView view = (ImageView) layout.getView();
      view.setImageBitmap(bmp);

do you want to create a ImageView ??

wow now i only have one error

AndroidRuntime:
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/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/Tester/Tester.java:51: error: cannot find symbol
    [javac]           in = container.$form().openAssetForExtension(this, imagePath);
    [javac]                                 ^
    [javac]   symbol:   method openAssetForExtension(Tester,String)
    [javac]   location: class Form
    [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] 1 error
    [javac] 1 warning

replace this with openAsset

i will try

still not work

package com.srrazmi.com;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.HVArrangement;
import com.google.appinventor.components.runtime.ReplForm;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.ViewUtil;
import java.io.IOException;



@DesignerComponent(version = 1,  description = "This Extension was created with the AppyBuilder Code Editor.<br>" + 
                   "Create your own here:<br><a href='https://editor.appybuilder.com' target='_blank'>https://editor.appybuilder.com</a><br>",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class Assest extends AndroidNonvisibleComponent implements Component {
    public static final int VERSION = 3;
    private Button addButton;
    private ComponentContainer container;
    private Context context;
    private Drawable defaultTextBoxDrawable;
    private float density;
    private boolean isRepl = false;
    private String picturePath = "";

    public Assest(ComponentContainer container2) {
        super(container2.$form());
        this.container = container2;
        this.context = container2.$context();
        this.density = container2.$form().getResources().getDisplayMetrics().density;
        if (this.form instanceof ReplForm) {
            this.isRepl = true;
        }
    }
  
        @SimpleFunction(description = "Create Image. Margin referred to arrangement. Picture in asset.")
    public void Image(HVArrangement layout, String picture, int index, int width, int height, int leftMargin, int topMargin, float rotate) {
        String str;
        Drawable drawable;
        FrameLayout disposicion = (FrameLayout) layout.getView();
        ImageView addImage = new ImageView(this.context);
        addImage.setId(index);
        addImage.setRotation(rotate);
        if (picture == null) {
            str = "";
        } else {
            str = picture;
        }
        this.picturePath = str;
        try {
            drawable = MediaUtil.getBitmapDrawable(this.container.$form(), this.picturePath);
        } catch (IOException e) {
            drawable = null;
        }
        disposicion.addView(addImage);
        FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) addImage.getLayoutParams();
        param.leftMargin = (int) (((float) leftMargin) * this.density);
        param.topMargin = (int) (((float) topMargin) * this.density);
        param.height = (int) (((float) height) * this.density);
        param.width = (int) (((float) width) * this.density);
        ViewUtil.setImage(addImage, drawable);
        addImage.setLayoutParams(param);
    }
}

This Code Will Work Fine

2 Likes

sur @Srrazmi he is talking about set image of a existing component. and you create a new component. Btw, it’s depend on the needs of @Salman_Dev

1 Like

He Can Modify It According His Needs

1 Like