Image Loader / Explorer Dialog Box (Android 10 update at bottom)

You should just be able to use my updated code, along with your original code. No need for the pimage to bitmap conversion. I have trimmed the permissions code to its bare essentials and it works with your code. Just set it to write_external_storage in caps, and assign the permission fail message you want.

1 Like

Yes, but only if I set my SDK target to 20. I want to be able to use all the other features provided by the newer versions. Your permissions class works OK

I tested the new code using the standard processing ide, sorry forgot to precise this. So no need to downgrade to android 20 apk just build it straight from the PC environment.

Thatā€™s odd. I only work with APDE, and it doesnā€™t except the original + your permissions class without setting to SDK 20.

Ok let me test further, can you check from your machine if it works from your pc (if android mode is installed).

full code

import android.net.Uri;
import android.database.Cursor;
import android.content.Intent;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.Manifest;
//import android.appcompat.app.AppCompatActivity;
import java.io.*;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import android.graphics.*;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
Activity act;
Context mC;
PImage img;
boolean img_loaded;
String s;
Permission eStorage;

void settings() {
  fullScreen();
}

void setup() {
  //verifyStoragePermissions();
  s = Environment.getExternalStorageDirectory().getAbsolutePath();
  eStorage = new Permission(this,"WRITE_EXTERNAL_STORAGE");
  println(s);
  background(#F9C454);
  fill(#4A3A16);
  textSize(38);
  textAlign(CENTER);
  text("Click on screen", width/2, height/3);
  act = this.getActivity();
  mC= act.getApplicationContext();
}

void draw() {
  if (img_loaded&&img!=null)  image(img, 0, 0);
}

void mousePressed() {  
  searchImage();
}

public void searchImage() {
  Intent intent = new Intent(Intent.ACTION_PICK, 
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  intent.setType("image/*");
  //Activity a = 
  act.startActivityForResult(intent, 1);
}

@Override 
  void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 1) {
    if (resultCode == Activity.RESULT_OK) {
      if (data != null) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = mC.getContentResolver().query(selectedImage, 
          filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String imgDecodableString = cursor.getString(columnIndex);
        cursor.close();
        println( imgDecodableString);
        img = loadImage( imgDecodableString);
        img_loaded = true;
      } else {
        println("Cancelled");
      }
    }
  }
};

public class Permission{
  
  PApplet parent;
  
  public boolean requestedPortraitImage = false;

  public Permission(PApplet pParent,String permissionName) {
    parent = pParent;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
  };

  public void onPermissionResult(boolean granted) {
    if (!granted) {
      PApplet.println("User did not grant camera permission. Camera is disabled.");
    }
  };

};

Sorry, Iā€™ve usb connection problems, and never got the virtual device working.

no worries, ive just attempted the code and it also works from apde you just need to set sketch permissions in the properties.

Sure, I know, I always do, but it doesnā€™t run on my phone.
Did you try my code? Setting to target 29?

Just to confirm are you setting this permission

Gives me the following screen

Of course. I am using APDE for 2,5 years now.

Im. Just super confusedā€¦

What version of android are you using, just seeing if we can isolate the problem. Iā€™m on emu 10 for huawei which i think is equivalent to android 10.

Real Android 10 device.
Did you desinstall the previous app, for a clean restart?
Are you using APDE ā€œapp modeā€ not ā€œpreviewā€?

Yeah in app mode not preview

Did you?..

OK, so in preview mode it does not workā€¦ Not sure why, in app mode it works even in apk 29. Not sure how to get preview mode working, does app mode also not work for you?

Nopeā€¦

Does it at least run but fails when you load a pic or does it not run at all?

Crashes as soon as I choose picture.

Really sorry no idea why I wrote 29 that was supposed to be on 26, 29 fails on my phone too.

Ps I think I tested with 29 after building once with 26 and that works, but after a clean install 29 fails.