Right now I can’t use Corona to write an app that asks for a photo.
As I’ve fed back to this thread, there’s a problem loading a .jpeg file to a newImage if the image is very wide or high, such as photos coming from pretty much any phone manufactured within the last two years. The solution is to make sure a smaller version of the image is loaded to newImage. One way could be to have an inSampleSize option for newImage so that only every nth pixel is read from disk to memory. The other is to do the same streaming the photo to a temporary image file.
Often, the images are around 3000x4000px, which is a few multiples of the screen resolution on most phones, which means you rarely need the full resolution photo for anything in an app.
On Android (note that the problem is generic, though!), there’s a way to handle this. I have a working InputStream example in Java that takes a big image and scales it coarsely, to write out a temporary scaled down jpeg file. Would that be possible to do in Lua?
Note that just to not crash the phone, you can’t process the image as an object, that the whole problem behind this. It will try to allocate 36MB or more memory and fail. So any solution would have to process parts of the image and write them out, for example a pixel line at a time. It uses InputStream and BitmapFactory.
InputStream is = this.getContentResolver().openInputStream(imageUriOrig); BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; o.inPreferredConfig = Bitmap.Config.ARGB\_8888; BitmapFactory.decodeStream(is,null,o); is.close(); int w=o.outWidth; int h=o.outHeight; if (bm3!=null) bm3.recycle(); int insamp=w/1000;if (insamp\<2) {insamp=2;} o.inJustDecodeBounds = false; o.inPreferQualityOverSpeed = false; o.inSampleSize=insamp; o.inPreferredConfig = Bitmap.Config.ARGB\_8888; o.inDither=false; //Disable Dithering mode o.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared o.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future o.inTempStorage=new byte[32 \* 1024];
If you don’t think this is possible in Lua, can you think of some workaround? As long as I can get the image loaded without crashing the device I can take it from there.
For this app, the photos are coming from the camera, and users have to remember to modify the resolution in the camera app to not crash my app. Is there a way to select a resolution or set maximum picture measurements? That would solve it for this app.
I’m unable to pay for Enterprise I’m afraid, so I can’t use plugins. If it turns out a plugin is required to make this work, could an exception be made so that asking for a photo will function in Corona?