Why i can't access images using regular java IO

Hi
I get the FileNotFoundException while I try to access the selected file (via UIImagePickerController) using the following code :

   imagePickerController.setDelegate(new UIImagePickerControllerDelegate() {
            @Override
            public void imagePickerControllerDidFinishPickingMediaWithInfo(UIImagePickerController picker, NSDictionary<String, ?> info) {
                /*
                for(String key : info.keySet()){
                    Foundation.NSLog("key= " + key + "|| value= " + info.valueForKey(key).toString()  );
                }
                */
                String filePath = info.valueForKey("UIImagePickerControllerImageURL").toString();
                //UIImagePickerControllerReferenceURL
                //UIImagePickerControllerImageURL
                File file = new File(filePath);
                Foundation.NSLog("selected file path = '" + filePath + "' size =" + file.length());
                try{
                    FileInputStream fos = new FileInputStream(file);
                    byte[] data = new byte[(int)file.length()];
                    fos.read(data);
                    fos.close();
                }catch (FileNotFoundException ex){
                    ex.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                picker.dismissViewControllerAnimatedCompletion(true,null);
            }

plist :

<key>NSPhotoLibraryUsageDescription</key>
<string>You can select photos to attach to reports.</string>

output:

2017-10-31 19:42:17.323831+0200 TestPods[3982:183310] selected file path = 'file:///Users/mac/Library/Developer/CoreSimulator/Devices/852F36BC-2E99-45C1-BED6-48FC7D29D36E/data/Containers/Data/Application/F8E30212-63B0-4091-8C51-9311B1CE246E/tmp/5F09202C-F5AC-4175-A0D4-5E07318C372A.jpeg' size =0
java.io.FileNotFoundException: file:/Users/mac/Library/Developer/CoreSimulator/Devices/852F36BC-2E99-45C1-BED6-48FC7D29D36E/data/Containers/Data/Application/F8E30212-63B0-4091-8C51-9311B1CE246E/tmp/5F09202C-F5AC-4175-A0D4-5E07318C372A.jpeg: open failed: ENOENT (No such file or directory)
	at libcore.io.IoBridge.open(IoBridge.java:462)
	at java.io.FileInputStream.<init>(FileInputStream.java:76)
	at com.example.maittarek.testpods.Main$2.imagePickerControllerDidFinishPickingMediaWithInfo(Main.java:157)
	at apple.uikit.c.UIKit.UIApplicationMain(Native Method)
	at com.example.maittarek.testpods.Main.main(Main.java:87)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
	at libcore.io.Posix.open(Native Method)
	at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
	at libcore.io.IoBridge.open(IoBridge.java:448)
	... 4 more
2017-10-31 19:42:17.840335+0200 TestPods[3982:183417] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

thanks

Hi,

The java file api works with paths, and you are passing a file:// URL. You need to convert it to a path eg. by stripping the file: from the beginning.

Best Regards,
Gergely

1 Like

thanks
I removed “file:///” and it works