I can detect faces by the help of Google's sample vision app FaceTracker. What I want to do is to take photo of detected face and download it to phone Gallery.
File imagesFolder = new File( Environment.getExternalStorageDirectory() , "FolderName" );
imagesFolder.mkdirs();
final File image = new File( imagesFolder , "/" + "IMG_" +System.currentTimeMillis() + ".png" );
File file = new File( Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
imgCap.takePicture(image, new ImageCapture.OnImageSavedListener() {
#Override
public void onImageSaved(#NonNull File file) {
String msg = "Pic captured at " + image.getAbsolutePath();
//storing the image in a folder in gallery
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE );
Uri contentUri = Uri.fromFile( image );
mediaScanIntent.setData( contentUri );
getActivity().sendBroadcast( mediaScanIntent );
Related
I am trying to execute following like following
Uri.parse(pictureImagePath) is
/storage/emulated/0/Pictures/20171125_131914.jpg
But it is returning null.
Am i missing something??
Please help!!!
Following is calling the camera:
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
following is the code
File imgFile = new File(pictureImagePath);
if (imgFile.exists()){
bitmapImage = null;
bitmapImage = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(Uri.fromFile(imgFile))), imgFile);
I am running a new Intent to access the camera in Android, but I'm getting this error:
getOutputMediaFileUri method not recognized
The code is below
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent,0);
Now this will store your camera captured images in MyImages directory in sdcard with image_001.jpg name.
Your code seems to come from this Android Camera tutorial.
It says there:
The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files.
And in that link, we find the code:
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
So add the above snippet to your code, and you should be good to go.
You haven't read correctly the article. You have to add below methods into your MainActivity.java
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
I have saved the video on SD card but its not showing in gallery
I can see the video in the sd card and its playing
I am using this method to get video uri and save it
public static Uri getVideoUri(Context context) {
Marapreferences marapreferences=Marapreferences.getInstance(context);
boolean ismedia=marapreferences.isMedia();
File file = null;
File file2 = new File(Environment.getExternalStorageDirectory()
+ "/mara_messenger/videos");
if (!file2.exists()) {
file2.mkdirs();
}
currentFileName = "" + System.currentTimeMillis() + ".mp4";
imageName = Environment.getExternalStorageDirectory()
+ "/mara_messenger/videos/" + currentFileName;
file = new File(imageName);
Uri imgUri = Uri.fromFile(file);
System.out.println("Image uri" + imgUri);
return imgUri;
}
File file=new File(uri);
MediaScannerConnection.scanFile(this, new String[] {file.getAbsolutePath()},
new String[]{"video/mp4"}, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri)
{
System.out.println("completed");
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(uri);
sendBroadcast(intent);
}
});
I can see after scan URI and path but still not showing on gallery
I am capturing image by camera app and want to store in Specific folder but image not storing in folder here is my code :
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/" + "my_data");
try {
if (!dir.exists())
if (dir.mkdir()) {
System.out.println("Directory created");
Toast.makeText(Add.this, "created dir",
Toast.LENGTH_LONG).show();
}
image_path = Environment.getExternalStorageDirectory().getPath() + "/my_data/" + aa + "_image.jpg";
File file = new File(dir,image_path);
uriImage = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
EDIT
This should work, if not please post logcat output.
String folderPath = Environment.getExternalStorageDirectory().toString()
+ java.io.File.separator + "my_data"
);
File dir = new File(folderPath);
if(!dir.exists){
dir.mkdirs();
}
imagePath = folderPath+java.io.File.separator+aa+"_image.jpg";
I am new to android. I am capturing video using following code:
final int REQUEST_VIDEO_CAPTURED = 1;
Long tsLong = System.currentTimeMillis() / 1000;
String ts = tsLong.toString();
String imagepath = Environment.getExternalStorageDirectory() + "/"
+ galleryStart + "/" + FolderName + "/" + ts + ".mp4";
File file = new File(imagepath);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,outputFileUri);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
If I haven't set path its work fine other wise it gives me error." unfortunately camera has stopped working." I am setting path for saving the videos in particular directory.
Camera App Doesn't make you dir which you asked for in your Uri. So try to make it first.
String imagepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + galleryStart + "/" + FolderName + "/" + ts + ".mp4";
File file = new File(imagepath);
try
{
if(file.exists() == false)
{
file.getParentFile().mkdirs();
file.createNewFile();
}
}
catch (IOException e)
{
Log.e(TAG, "Could not create file.", e);
}
Uri outputFileUri = Uri.fromFile(file);
Hope it helps you out!!