java.io.FileNotFounfException:/images/captured.jpg: open failed:ENOENT

28 views Asked by At

I am trying to save a picture captured from camera temporarily in android using the getUriForFile in FileProvider.

My app takes the images from the camera and I am trying to store these images in a temporary folder until the user clicks on the upload button and the stored images are sent to a database through an API using retrofit2.

I receive the filenotfoundexception in a toast on the emulator when I click on the upload button.

The logcat doesnt give any errors on clicking upload button except: File error accessing recents directory (directory doesn't exist?). The images are stored in Bitmap format in the imageView as I want to see whether the capture button is working or not.The Imageview feature is going to be deleted later.

Please can someone help!

I have tried using new File(this.getApplicationContext().getExternalFilesDir(null).toString()); and Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); but the error still persists.

I checked using file.createNewFile(); but still nothing.

here is my saveImg function:

   private Uri saveImg(Bitmap imageBit, Context context) {
    
    File imgFolder=new File(context.getFilesDir(),"images");
    
    if (!imgFolder.exists()) {
        boolean created = imgFolder.mkdirs();
        if (!created) {
            throw new RuntimeException("Failed to create directory: " + imgFolder.getAbsolutePath());
        }
    }    
    Uri imgUri = null;
    File file = null;
    try {            
        //addition of .getabsolutepath
        file = new File(imgFolder, "captured.jpg");
        FileOutputStream stream = new FileOutputStream(file);
        imageBit.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
        
        imgUri = FileProvider.getUriForFile(context, "com.example.login_page_simple"+".provider", file);
        Log.i("VIDEO RECORD TAG", "Image is captured and available at path" + imgUri);
        
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        
        throw new RuntimeException(e);
    }
    return imgUri;
}` 

here is the code for upload button:

public void uploadImageWithUsername(String title,String description,String web_link,String app_link,String status){
Uri imgUri=saveImg(imageBit,CameraActivity.this);
    File file=new File(imgUri.getPath());

RequestBody requestFile=RequestBody.create(MediaType.parse("multipart/form-data"),file);
MultipartBody.Part body=MultipartBody.Part.createFormData("img",file.getName(),requestFile);
RequestBody titl=RequestBody.create(MediaType.parse("multipart/form-data"),title);
RequestBody cus_des=RequestBody.create(MediaType.parse("multipart/form-data"),description);
RequestBody wL=RequestBody.create(MediaType.parse("multipart/form-data"),web_link);
RequestBody aL=RequestBody.create(MediaType.parse("multipart/form-data"),app_link);
RequestBody stat=RequestBody.create(MediaType.parse("multipart/form-data"),status);
Call<Image> call=RetrofitClient.getInstance().getInterfaceAPI().uploadImageWithUsername(body,titl,cus_des,wL,aL,stat);
call.enqueue(new Callback<Image>() {
    @Override
    public void onResponse(@NonNull Call<Image> call, @NonNull Response<Image> response) {
        if(response.isSuccessful()){
            assert response.body() != null;
            if(response.body().getStatus().toString().equals("200")){
            //previous_context: CameraActivity.this
                Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_SHORT).show();
                Log.i("VIDEO RECORD TAG","Image is captured and available at path");
            }
            else{
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();

                Log.i("VIDEO RECORD TAG","Image couldnt be captured");
            }
            //String status = response.body().getStatus();

            // Assuming getImage() returns the MultipartBody.Part containing image data
            //MultipartBody.Part imagePart = response.body().getImage();
        }
    }

    @Override
    public void onFailure(@NonNull Call<Image> call, @NonNull Throwable t) {

        Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_SHORT).show();
    }
});
}
0

There are 0 answers