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

The extension writes the image to the external storage, and returns the path to the file written in the filePath parameter.

1 Like

sur @mohamed_tamer
i just send you a PM

can i join :sweat_smile:

sorry, bru
he is offline.
i will tell him to join you

i get an error when trying the code from @Mohamed_Tamer

 @SimpleFunction
    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();
      view.setImageBitmap(bmp); 
    }

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/Tester/Tester.java:43: error: cannot find symbol
    [javac]            in = container.$form().openAssetForExtension(imagePath);
    [javac]                                  ^
    [javac]   symbol:   method openAssetForExtension(String)
    [javac]   location: class Form
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/salmanappinventor/Tester/Tester.java:50: error: cannot find symbol
    [javac]       view.setImageBitmap(bmp); 
    [javac]           ^
    [javac]   symbol:   method setImageBitmap(Bitmap)
    [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
1 Like

you missing a import - import android.view.View;

I’ve added it

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.appinventor.components.annotations.;
import com.google.appinventor.components.runtime.
;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.InputStream;

can i have your full code

i think you import it wrongly

import com.google.appinventor.components.annotations.;
import com.google.appinventor.components.runtime.;

instead of this. Use this code.

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
1 Like

still not work :thinking:

. …

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;
import java.io.InputStream;

@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 Tester extends AndroidNonvisibleComponent {
    private ComponentContainer container;
    /**
     * @param container container, component will be placed in
     */
    public Tester(ComponentContainer container) {
        super(container.$form());
        this.container = container;
    }
  
   @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();
      view.setImageBitmap(bmp); 
    }

}

Have You Tried Thisone

@SimpleFunction(description = "")
public void pick(AndroidViewComponent layout, String imagePath) {
try {
    InputStream ims = getAssets().open(imagePath);

    Drawable d = Drawable.createFromStream(ims, null);
View view = layout.getView();
 view.setImageDrawable(d);
}
catch(IOException ex) {
   ///Some Code
}

Oky thank you i will try

As I Know You Can’t Set Any Source To View Without Decoding InputStream So We Need To Import Stream Reader And Decode It Using This And StreamReader Needs BufferReader So We Need To Read Stream Insite BuffereReader After If All Done So We Got File Source To Set in View :joy: :joy: :joy:

So, what should I do? what should i import? and what code I can use?
Mr.@Srrazmi

The Better Way Is Try With Custom Source Url

InputStream in;
try{
in = container.$form().openAssetForExtension(this, "myAssetName");
} finally {
in.close();
}

Bitmap bmp = BitmapFactory.decodeStream(in);

what’s the problem with this code ??