Android camera .. save image


#1

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");
}

#2

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


#3

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


#4

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

```processing
// your code goes here
```

#5
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”);


}