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:

    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


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()) {
            return null;
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        byte[] bytes = new byte[buffer.remaining()];
        FileOutputStream output = new FileOutputStream(file);
        return file;

My last code:

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

            if (!appDirectory.isDirectory())

            // 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.