Android camera .. save image

hi,

Im trying to saving the image from my android camera …
i search an found this code …
its work to make a sub directory … but didnt save the image on it !

whats wrong with my code … help
thanx

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
 
Intent intent; 
Uri file; 
public static final int LAUNCH_CAM =2;
public static final int CAMERA_REQUEST = 1;
//static int RESULT_OK;
 
public void setup(){
  orientation(LANDSCAPE);
  Intent intent= new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
  file = Uri.fromFile(getOutputMediaFile());
  println(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
    getActivity().startActivityForResult(intent, LAUNCH_CAM);
  
  
}
 
 
public void onActivityResult(int requestCode, int resultCode,Intent data)
    {
                 
      if (requestCode == LAUNCH_CAM && resultCode == Activity.RESULT_OK){
       
        println("tout est ok------------------");
    }
  }
 
 
public void draw(){
 
}


private static File getOutputMediaFile(){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "CameraDemo");
 
    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            return null;
        }
    }
 
    String timeStamp = "hhhh";
    return new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
}

Can you do instead:

String fn=mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg";
println("Saving image at ",fn);
return new File(fn);

And verify the File field is being returned? Also, did you enable the required permission? Not sure if there is also a permission for the camera… just dbl check.

Kf

this is the print : “file:///storage/emulated/0/Pictures/CameraDemo/IMG_20180605_220725.jpg”

I found something that i should replace
file = Uri.fromFile(getOutputMediaFile());
to
Uri file= FileProvider.getUriForFile(this, “com.example.android.fileprovider”, file);

file:// is not allowed anymore … we should send the URI through content:// scheme instead which is the URI scheme for Content Provider.

I replace … and have the error : The name “FileProvider” cannot be recognized.
i add : import android.support.v4.content.FileProvider; and have error : The import android.support.v4 cannot be resolved

and what is the “com.example.android.fileprovider” ??

any idea ?

thanx

Please format your code with 3 backticks, to make it easier to read:

```processing
// your code goes here
```
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

Intent intent;
Uri file;
public static final int LAUNCH_CAM =2;
public static final int CAMERA_REQUEST = 1;
//static int RESULT_OK;

public void setup(){
orientation(LANDSCAPE);
Intent intent= new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
file = Uri.fromFile(getOutputMediaFile());
println(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
getActivity().startActivityForResult(intent, LAUNCH_CAM);

}

public void onActivityResult(int requestCode, int resultCode,Intent data)
{

if (requestCode == LAUNCH_CAM && resultCode == Activity.RESULT_OK){

println("tout est ok------------------");

}


}

public void draw(){

}

private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), “CameraDemo”);

if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}

String timeStamp = “hhhh”;
return new File(mediaStorageDir.getPath() + File.separator +
“IMG_”+ timeStamp + “.jpg”);


}

Anyone have idea on how to get this done?

@adaptinnovative ===
I have written the code initially put; i have tested it again (OS6 MM), with some little modifs and it works: i can see the folder “camerademo” inside PICTURES and i can see the photo inside it. It is possible that URIfromFile does not work for OS>6 MM, yet you can try and it s esay to change this part of the code. Dont forget to ask permissions on rutime if you using a phone more than lollypop.

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
 import android.os.Environment;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
int MyVersion = Build.VERSION.SDK_INT;
Intent intent; 
Uri file; 

public static final int CAMERA_REQUEST = 1;

Activity act;
boolean accordees = false;
 
public void setup(){
  orientation(LANDSCAPE);
  size(displayWidth, displayHeight);
  act = this.getActivity();
 ///here ask for permissions: CAMERA, WRITE_EXTERNALSTORAGE on rutime, not only through the Manifest
  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    
  Uri file = Uri.fromFile(getOutputMediaFile());
  println(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
  act.startActivityForResult(intent,CAMERA_REQUEST  );

};
  
  
public void onActivityResult(int requestCode, int resultCode, Intent data)
   {
      super.onActivityResult( requestCode, resultCode,  data);
        
      if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
      println("camera ok); 
        
    }
      
  };
 
 
public void draw(){
 
}


private static File getOutputMediaFile(){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "CameraDemo");
 
    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            return null;
        }
    }
 
   
    return new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ System.currentTimeMillis() + ".jpg");
};