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

I believe it works for you because you are on an A10 emulator.
Sorry for repeating, but you still didn’t answer my question.

Is emu 10 for huawei not actually android? Sorry what question?

EMUI is a software update from Huawei based on Google’s Android OS platform. The EMUI 10 version of the custom skin is based on Android 10 , and includes many of the OS’s features along with some proprietary elements and a new user interface from Huawei .26 Feb 2020

Soz just rereading the thread I thought I asked you to test 29…

So you’re code works but not on 29

Is that a question?..
I mean you’ve set to 29 and also gave permissions?

Fresh install and it works on 29. I was building over the last version and it kept saying cannot delete old apk but now it works. Although it never prompts me to give permission

My code?..

this one

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.graphics.Bitmap; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import android.os.Environment; 
import android.graphics.BitmapFactory; 
import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.os.Build.VERSION_CODES; 
import processing.core.PConstants;

Activity activity; 
Context context; 
PImage img; 
boolean image_loaded; 

void setup() { 
  fullScreen(); 
  background(#F9C454); 
  fill(#4A3A16); 
  textSize(38); 
  textAlign(CENTER); 
  text("Click to open Image Explorer", width/2, height/3); 
  activity = this.getActivity(); 
  context = activity.getApplicationContext(); 
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { 
    requestParticularPermission();
  }
} 

void draw() { 
  if (image_loaded) {
    background(255);
    image(img, 0, 0);
  }
} 

void mousePressed() { 
  openImageExplorer();
} 

@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 image_uri = data.getData(); 
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(image_uri, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String imgDecodableString = cursor.getString(columnIndex); 
        cursor.close();
        println(imgDecodableString); 
        if (Build.VERSION.SDK_INT >= 28) {
          try { 
            InputStream ips = context.getContentResolver().openInputStream(image_uri); 
            Bitmap bitmap = BitmapFactory.decodeStream(ips);
            img = new PImage(bitmap.getWidth(), bitmap.getHeight(), PConstants.ARGB);
            bitmap.getPixels(img.pixels, 0, img.width, 0, 0, img.width, img.height);
            img.updatePixels();
            image_loaded = true;
          }
          catch (Exception e) { 
            e.printStackTrace();
          }
        } else { 
          img = loadImage(imgDecodableString); 
          image_loaded = true;
        }
      } else {
        println("No data");
      }
    }
  }
} 

private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; 
private void requestParticularPermission() { 
  activity.requestPermissions(PERMISSIONS_STORAGE, 2020);
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
  switch (requestCode) { 
  case 2020: 
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      println("permissions granted");
    } else { 
      println("permissions not granted");
    } 
    break; 
  default: 
    activity.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }
}

void openImageExplorer() {
  Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
  intent.setType("image/*"); 
  activity.startActivityForResult(intent, 1);
}

OK, thanks…

Gonna have to go I’ve been stood outside for 1/2 hr and am freezing. Let me know if you wanna test anything else I can understand why it doesn’t work

hehe. good night…

ok so have gone over all the code again.

your new code works with all sdk versions but never prompts for permission, and does not require the manifest to have the permissions in question.

as an alternative I have combined some of the code with my last suggestion.

this also works with all sdk versions but provides a prompt, and also again requires permission be set in the manifest.

as you are more familiar with apde than myself I’d recommend going with @noel version.

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.");
    }
  };

};
1 Like

This is probably because you didn’t deinstall the app before because when I do that, it asks again.

Reinstalled apde and tried again. It doesn’t require a prompt if the manifest doesn’t include the permission, but will still load the file regardless, though it does print permissions not granted in the console. And it will prompt if I add permissions to the manifest.