Compile error with byte[]

Here the code which I use in my PkgUtils extension to get application's icon:

public String Icon(String packageName) {
    try {
        FileOutputStream fileOutputStream = null;
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String ID = AppName(packageName);
        File file = new File(path, ID + ".png");
        fileOutputStream = new FileOutputStream(file);
        Bitmap img = getBitmap(packageName);
        img.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        return String.valueOf(file);
    } catch (Exception e) {
        return String.valueOf(e);
    }
}

public Bitmap getBitmap(String packageName){
    Bitmap img = null;
    try {
        Drawable icon = context.getPackageManager().getApplicationIcon(packageName);
        BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
        img = bitmapDrawable.getBitmap();
    }catch (Exception e){
        e.printStackTrace();
    }
    return img;
}

Oh thanks very much vknow36 with your code i did lots of progress:
it work fine and i succed to obtain album art by saving it in a file, passing path to extention and see it in a layout !

the only problem still existing is that it works well throug companion but as soon as i built APK it doesn't work any more...

it catch an exeption here:

file = new File("/storage/emulated/0/jmlSongImage.png");
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
reponse=path;
} catch (Exception e) {
e.printStackTrace();
reponse="erreur";
}

the problem is due to outputStream = new FileOutputStream(file);
even if i delete bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
I have "erreur" in reponse ...

i'm sure it's just a detail, because it works very well with companion !

The problem is that you're saving it to an absolute path on the emulated (external) storage. If the compiled app doesn't have the WRITE_EXTERNAL_STORAGE permission and request it at runtime prior to writing the file, the app will fail. Instead, you could write it to a relative file, which writes it to the app-private data directory, and then return the relative path for use in the Image or arrangement components.

Yes i thought it was something like that...
but i don't really know how define a relative path !!!
could you give me the code for it ?

It should just be a matter of not including any "/" in the filename, e.g.,

try {
  FileOutputStream out = new FileOutputStream("temp.png");
  ...
} catch (IOException e) {
  ...
}

ok. i tried it right now...

no it doesn't work ...
and it does'nt work with companion any more...

What if you try something like:

try {
  String filename = form.getCacheDir().getAbsolutePath() + "/temp.png";
  FileOutputStream out = new FileOutputStream(filename);
} catch (IOException e) {
  ...
}

it seems there is no error any more, because my code returns :
"/data/user/0/edu.mit.appinventor.aicompanion3/cache/temp.png" with companion
or "/data/user/0/appinventor.ai_jm_latour.imagesMp3/cache/temp.png" with built

but i don't really know if temp.png exists, because i can't go there and see it with my file explorer, and i can't use it in AI with blocks to display image file:



I also try to get it with "temp.png", "/temp.png", "//temp.png" but none of them display image file...

It should work with the whole absolute path. As an alternative so you can inspect the file, you could use form.getExternalCacheDir(), which will dump the file into /storage/emulated/0/Android/.../cache rather than putting it in the private directory.

YEEES !

here it is !

I don't know why other solutions doesn't work but this one do...
thank yo very much ewpatton it would be impossible to succed without your help !

Cache dir has a problem: i need to move file to Files dir in order to be able to put it in an image...
which is the instruction to save it directly in files dir in my extension ?