I was wondering if anyone knew a good way to load an image from a phone’s gallery into a PImage in a sketch. Working with an application in Android Studio.
I’m trying to load an image from the photo gallery. I was using a photoPickerIntent to have the user select a photo from the gallery, saving the filepath to that image as a string, then passing the string into the sketch as a parameter, and attempting to use loadImage() with that string.
At that point I am getting the ‘File contains a path separator’ error, which from what I’ve seen in other threads is a common occurrence, but I can’t find a solution that matches this problem. Is it possible to loadImage() from outside of the ‘data’ folder, specifically the gallery, or is the solution some way to save the selected image to the data folder during runtime so the loadImage() function can access it easily?
I’ve tried using the code that this user got to work : https://discourse.processing.org/t/show-image-stored-in-sdcard-sdk-26-solved/4397/11 , however still no luck. In this attempt, I passed the selected image’s uri into the sketch, created a file object using the uri, .toString()-ed the file, and fed that to loadImage(), same path separator error.
I know I can get a Bitmap from the image selected in the gallery and pass that into the sketch (not memory efficient, but better than nothing), is there perhaps any good way to convert that into a PImage object?
I’m really open to any solution here.
Currently from the photoPickerIntent I’m getting the image uri in OnActivityResult:
Uri imageUri = data.getData();
Then I’m putting it as an extra to the next activity that runs the sketch:
Intent i = new Intent(SelectImageActivity.this, GenerateLevelActivity.class);
i.putExtra("image_file_path", passableUri.toString());
startActivity(i);
In that next activity I retrieve the extra and pass it into the sketch object:
Uri imageUri = Uri.parse(getIntent().getStringExtra("image_file_path"));
sketch = new level_generation_sketch(imageUri);
Then in the sketch I’m basically running the code of the previously mentioned user:
PImage img;
Uri imageUri;
level_generation_sketch(Uri selectedImage){
imageUri = selectedImage;
}
public void setup(){
size(1280,720);
requestPermission("android.permission.READ_EXTERNAL_STORAGE", "initRead");
requestPermission("android.permission.WRITE_EXTERNAL_STORAGE", "initWrite");
}
public void draw(){ }
void initRead(boolean granted) {
if (granted) {
println("init read storage OK");
File f = new File(imageUri.getPath());
String myPath = f.toString();
img = loadImage(myPath);
image(img, 0, 0);
} else {
println("Read storage is not available");
}
}
void initWrite(boolean granted) {
if (granted) {
println("init write storage OK");
} else {
println("Write storage is not available");
}
}
Any help is appreciated.