Set Image output from extension

Imports:

import android.content.Context;
import android.content.res.AssetManager;

And add:

Context context = container.$context();

ok so i'm here:

@DesignerProperty()
    @SimpleProperty(description = "Sets the Image ")
    public void Image(String image) {
		Context context = container.$context();
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(image);
		this.globalBitmap = BitmapFactory.decodeStream(inputStream);
		this.globalBitmapSet=true;
    }

with compil error:

error: cannot find symbol
[javac] InputStream inputStream = assetManager.open(image);
[javac] ^
[javac] symbol: class InputStream
[javac] location: class EditImage
[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.

import java.io.InputStream;

oh yes ! i had OutputStream, but not InputStream !!!

ok, i got it !!

this is the final code..and i can modify my image !

    @DesignerProperty()
    @SimpleProperty(description = "Sets the Image ")
    public void Image(String image) {
		Context context = container.$context();
        AssetManager assetManager = context.getAssets();
		try{
        InputStream inputStream = assetManager.open(image);
		this.globalBitmap = BitmapFactory.decodeStream(inputStream);
	}catch (Exception e) {}
		this.globalBitmapSet=true;
    }
	@DesignerProperty()
    @SimpleProperty(description = "")
	public String test(){
		if(this.globalBitmap!=null)return "ok";
		else return "null";
	}

Thanks a lot TIMAI2 and Profile - Patryk_F - MIT App Inventor Community !
I didn't think it was so complicated...but it works now, i will able to add this in my extension.

This works only if the image is in assets.

You only want to edit image from assets?

no, if you use this block
component_set_get(1)
or this king of blokcs
image

you can edit an image file

I think you should standardize it. One property should handle all types of paths. Additionally, the code must include support for resources from the companion app for the extension to work with the companion.

I don't know how to do that !

I will try this

it works with gif too.

1 Like

done. i modify my first post.

Getting better. I have a question, if there is a property to set the image source for modification, why do a large number of blocks have an image path parameter?

they allow you with one block to define image path, do one modification and get result.

1 Like

In order to prevent people to set file wich is not an image, i tried to follow this

but when i try it:

   public void Image(@Asset({".png", ".jpeg", ".jpg"}) String image) {
		if(image.startsWith("file:///") || image.startsWith("/")){	//si chemin de fichier
			String filePath=defineDir(image);

i got an error:

error: cannot find symbol
[javac] public void Image(@Asset({".png", ".jpeg", ".jpg"}) String image) {
[javac] ^
[javac] symbol: class Asset
[javac] location: class EditImage
[javac] 1 error

what is the problem ?

Have you imported Asset annotations? Are you using Rush? Which version?

Ok, I see you don't use Rush. And I see that you import each annotation separately, so just add import for Asset.

it's not that ? import android.content.res.AssetManager;
because i already have it !

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

error: cannot find symbol
[javac] import com.google.appinventor.components.annotations.Asset;
i tried with
import com.google.appinventor.components.annotations.*;

but again
error: cannot find symbol
[javac] public void Image(@Asset({".png", ".jpeg", ".jpg"})String image) {

Where and how do you compile the extension? Maybe you have cloned a very old AppInventor source?

I compile it with Git.
may be an old version of AppInventor source: 16/04/2014...
should i download a new one ?