In my app, I need to take a photo, then send it via RestApi with other data. I want to store this photo in a different location to process it in the future. My code:
private void TakePictureAfterScan() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
TAKEN_PHOTO = Uri.fromFile(getOutputMediaFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, TAKEN_PHOTO);
startActivityForResult(takePictureIntent, ACTIVITY_CODE_REQUEST_IMAGE_CAPTURE);
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), PICTURE_PHOTO_DIR);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
}
When I set a breakpoint in 'onActivityResult' method (which is empty right now for tests), there are already two .jpg files saved in the device:
In DCIM/Camera folder
In my temporary folder
The problem is, both files have different names. I do not need and do not want files from DCIM/Camera folder. Is there any simple way to save taken a photo only in my temporary folder?
instead of this .
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), PICTURE_PHOTO_DIR);
use this..
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString() + "folder name" + ImageName;);
Related
I would like, as title suggests to find an image via Android File Explorer. I'm taking a photo and saving in on external storage but that part of storage is only available to app itself.
Basically, before I start Camera activity I have following code:
public File getPhotoFile(Contact contact, Context context) {
File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (externalFilesDir == null) {
return null;
}
File f = new File(externalFilesDir, contact.getPhotoFilename());
return f;
}
Method in Contact class getPhotoFilename():
public String getPhotoFilename() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return "IMG_" + this.getId() + "_" + timeStamp + ".jpg";
}
And in activity:
cameraButton.setOnClickListener((View v) -> {
File f = getPhotoFile(newContact, this);
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.email.fileprovider",f);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(captureImage, REQUEST_TAKE_PHOTO);
});
I display full path, like this:
Log.i("PATH", filePath); //file path is member variable which just holds f.getAbsoluthPath();
What I get in output is:
/storage/emulated/0/Android/data/com.example.email/files/Pictures/IMG_0_20200428_001544.jpg
I've tried find that image in File Device Explorer, but no luck.
Currently i am taking image from camera and gallery . When i take picture from camera then the path is storage/sdcard0/pictures/androidfileupload/.
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Android File Upload");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
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 {
return null;
}
return mediaFile;
}
so from the above code i am fetching storage/sdcard0/pictures/android file upload/ .
Now i am taking selected image from gallery so it shows error like storage/sdcard0/pictures/androidfileupload/imagename.jpg is not found .
I have debug the code and i get the path of selected image from gallery content://media/external/images/media/26435 but i want the path like
content://media/external/images/media/imagename.jpg.
so how can i get the path with imagename.jpg.?
Hello I'm developing an app which uses the camera2 API. I used the Caemra2Video sample provided by Google, and during the modification I changed the file storage directory.
The original code is like this:
mMediaRecorder.setOutputFile(getVideoFile(activity).getAbsolutePath());
...
private File getVideoFile(Context context) {
return new File(context.getExternalFilesDir(null), "video.mp4");
}
I changed it in this way:
private File getVideoFile(Context context) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "camera2Video");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File videoFile;
videoFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
return videoFile;
}
If I record a video, there is indeed a file with the correct name inside the folder. However, whenever I open the app and start the camera activity, there will be a .mp4 file generated (no matter whether I actually record the video or not) with the size of 0. How can I modify it to avoid this? Thanks!
I'm getting this error "Cannot resolve constructor 'Date()'" and i don't know why.
If someone could explain why i'm getting it, i would be grateful.
Thank you
Code:
/** Create a File for saving an image or video */
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="camera_wiki"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
The issue is resolved by using a correct Date class: java.util.Date.
I am capturing image via my application and I successfully stored in sdcard folder which I had created for my application. Now I need to retrieve the stored image in list view. Saving the images in sqlite is not an efficient way. Can any one direct me in any other way on how to save and retrieve like using path.
Am capturing picture here:
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
And I have also created the directory in sd card to save my captured images via my application below:
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;
}
}
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 {
return null;
}
// TODO Auto-generated method stub
return mediaFile;
}
This link will give you how to get the image path and save it in DatabaseLink
And after retrieving the file path, you'll have to load image from that path.
To load image from file path,this link will help you.