How to fix - File cannot be converted to String?

Hi all, :slight_smile: :pray:

Im try make another Camera extension...

i get error :thinking:

error: incompatible types: File cannot be converted to String
[javac] Cam.saveImage(image, file);
[javac] ^"...

my code:

@SimpleFunction
    public void SavePicture(){
        dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.getDefault());
	String filename = "/Pictures/app_inventor_"+dateFormat.format(new Date())+".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), filename);
	Cam.saveImage(image, file);
    }

Please help to fix :slight_smile: :pray:

Thank you very much before

regard,
kangris

It’s really hard to know without knowing what the Cam object is, but my guess is that it’s expecting a String that is a path to the file, in which case you’ll want to call file.getAbsolutePath().

1 Like

Thanks for replay sir @ewpatton :slight_smile:

I look function Cam.saveImage();
from code library :

/**
     * Save image to storage
     * @param image Image object got from onPicture() callback of EZCamCallback
     * @param file File where image is going to be written
     * @return File object pointing to the file uri, null if file already exist
     */
    public static File saveImage(Image image, File file) throws IOException {
        if(file.exists()) {
            image.close();
            return null;
        }
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        FileOutputStream output = new FileOutputStream(file);
        output.write(bytes);
        image.close();
        output.close();
        return file;
    }

My last code:

@SimpleFunction
    public void SavePicture(){
	// Create directory.
            File externalStorageDirectory = Environment.getExternalStorageDirectory();
            File appDirectory = new File(externalStorageDirectory.getAbsolutePath() + "/Pictures/app_inventor");

            if (!appDirectory.isDirectory())
                appDirectory.mkdirs();

            // Create file name.
            String fileName = String.format(Locale.ENGLISH, "%d.jpg", System.currentTimeMillis());
            File imageFile = new File(appDirectory, fileName);

            try {
                // Save the image in device.
                Cam.saveImage(image, imageFile);
	    } catch (IOException e) {

            }
    }

Do you experience the same error since you’ve changed the code? The signature looks correct to me, but without seeing everything it’s hard to tell why you’d be getting type errors in this context unless image isn’t of type Image.