I am currently trying to create a button the opens the camera app, and saves the picture with a specific name and location.
this is used to create the name/directory
// create a filename for image to be stored into
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String Calib_image_File_Name = "Calibration" + "_" + timeStamp + "STOP";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
Calib_image_File_Name, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
And this is my handler
public void Take_Image_calib(View view)
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
// ...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
The issue i have is that after the "STOP" , it adds a 19 digit number afterwards. i don't know why this is happening, advice?
EDIT:
File provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"></meta-data>
</provider>
This is happening because of the createTempFile in its definition it has this line:
...new File(tmpDirFile, prefix + tempFileRandom.nextInt() + suffix);
which I believe causes the problem. Simply use new File(...):
File image = new File(storageDir, Calib_image_File_Name + ".jpg");
Related
I need your help.
I'm starting to create an app, and I would save photos, videos, vocal memo etc. like a post/note and everything are part of this post.
These multimedia files, will be saved in a folder, which name is a timestamp. On this way, every post/note is charaterized by the timestamp. The timestamp should update every time when I want to create a new post.
I want to save my files by following this path:
Name_app
Timestamp
Pictures
Pictures1
Pictures2
...
Videos
Video1
...
Below my code.
This is the code which create my Image File (and it look like to work):
private File createImageFile () throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File root = new File(Environment.getExternalStorageDirectory().toString());
File myDir = new File(root + "/Urban_stories_sharing/" + timeStamp + "/Pictures");
if (!myDir.exists()) {
myDir.mkdirs();
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
myDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
This is the code which start the activity:
public void goToCamera(View v) {
getCameraPermission();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
isStoragePermissionGranted();
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.andreacarubelli.urbanstoriessharing.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
I think that the problem is on "Uri photoURI = FileProvider.getUriForFile(this,
"com.example.andreacarubelli.urbanstoriessharing.fileprovider",
photoFile);"
This is the code of my path (and I don't know how to change it dynamically like this -> "Urban_stories_sharing/" + timeStamp + "/Pictures"):
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Urban_stories_sharing/Pictures" />
</paths>
This is my provider in my Android Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.andreacarubelli.urbanstoriessharing.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths">
</meta-data>
</provider>
Thank you so much.
I am developing an application that captures photo and saves it in internal device folder named "photo".I need to load the photo from "photo" folder to imageview.There is only one photo present in the folder.How to do this?
Please help me.Thank you in advance.
Below is the code that I have tried which loads photo from folder only if name of photo is displayed.
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/GeoPark/final_photo/20180504_002754.jpg";
File imgFile = new File(path);
if (imgFile.exists()) {
imageView.setImageBitmap(decodeFile(imgFile));
}
else
Toast.makeText(ViewActivity.this,"No Image File Present ",Toast.LENGTH_SHORT).show();
Try this method .. in below method give proper file path.
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Second things ..
File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);
alos i hope you add below permission into android manifest file ..
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
You could simply do a search about this, guaranteed results.
Anyway, Glide will help you with that
EDIT :
Taking picture using camera:
private void dispatchTakePictureIntent(Context context) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(context);
} catch (IOException ex) {
// Error occurred while creating the File
Snackbar.make(btn_open_camera, "Couldn't create a file for your picture!", Snackbar.LENGTH_LONG);
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(context,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, MY_PERMISSIONS_REQUEST_CAMERA);
}
}
}
private File createImageFile(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getFilesDir();
Log.i(TAG, "StorageDir : " + storageDir);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
Log.i(TAG, "mCurrentPhotoPath : " + mCurrentPhotoPath);
return image;
}
Now you have the image path stored in mCurrentPhotoPath which is a String declared in current activity. As you said, you will need this in next activity, to do that you can take a look at this answer
Edit
My functions now look like this:
p
If uou want to save image in your external storage use Externalstoragepublicdirectory function.
This intent expects the EXTRA_OUTPUT location in Uri format, see https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE. Some devices may understand an absolute file path, but this is not the documented behavior.
You can use getExternalMediaDirs() to avoid many permission restrictions.
I have made some changes to your code:
Add this to you application level in your Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Then create file_paths.xml file:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="my_cache" path="." />
<external-path name="my_images" path="Pictures/" />
</paths>
Then in your code:
private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
private String mTempPhotoPath;
public void takePhoto() {
// Create the capture image intent
Intent imageCapture= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (imageCapture.resolveActivity(getPackageManager()) != null) {
// Create the temporary File where the photo should go
File photoFile = null;
try {
photoFile = createTempImageFile(this);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
// Get the path of the temporary file
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri imageUri = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
imageCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}
static File createTempImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalCacheDir();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
}
//To save image
String mCurrentPhotoPath = null;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/DCIM/Camera");
Log.d(TAG, "createImageFile: Saving image to:" + storageDir);
File image = new File(storageDir, imageFileName);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
//Return the saved image
return mCurrentPhotoPath ;
}
I am in trouble with this issue, couldn't find any way out yet.
I have configured a Fileprovider in manifest.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.package.name.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
where the #xml/file_paths is like
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="my_images"
path="Android/data/com.package.name/files/Pictures" />
<external-files-path
name="my_images_" />
</paths>
And my java code is like below
private void selectImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("selectImageFromCamera", "Error: " + ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
try {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.package.name.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
e.printStackTrace();
Log.e("selectImageFromCamera", "Error: " + e);
}
}
}
The method createImageFile()
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.e("storageDir", "storageDir: " + storageDir.getAbsolutePath());
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
myCurrentPhotoPath = image.getAbsolutePath();
return image;
}
But I am always getting problem when creating photoUri, to be exactly at below line.
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.package.name.fileprovider",
photoFile);
Error in try catch
.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.package.name/files/Pictures/JPEG_20170417_115925_834197983.jpg
Any help would be appreciated. Thanks.
I am going to take a photo from Camera in Xamarin Android.
Here's what I did in Xamarin.
Start Camera Activity
private void CallTakePictureIntent ()
{
Intent takePictureIntent = new Intent (MediaStore.ActionImageCapture);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.ResolveActivity (PackageManager) != null) {
// Create the File where the photo should go
Java.IO.File photoFile = null;
try {
photoFile = ImageHelper.CreateImageFile (this);
} catch (Exception ex) {
System.Console.WriteLine (ex.ToString ());
}
// Continue only if the File was successfully created
if (photoFile != null) {
sURI photoURI = FileProvider.GetUriForFile (this, UIHelper.FileProvider, photoFile);
requestedAvatarUri = photoURI;
takePictureIntent.PutExtra (MediaStore.ExtraOutput, photoURI);
StartActivityForResult (takePictureIntent, REQUEST_TAKE_PICTURE);
}
}
}
Create temporary file.
public static Java.IO.File CreateImageFile (Context context)
{
// Create an image file name
string timeStamp = DateTime.Now.ToString ("yyyyMMdd_HHmmss");
string imageFileName = "JPEG_" + timeStamp + "_";
Java.IO.File storageDir = context.GetExternalFilesDir (Android.OS.Environment.DirectoryPictures);
Java.IO.File image = Java.IO.File.CreateTempFile (
imageFileName, /* p refix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
Android.Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.masterbee.xamapp-dev.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
xml/file_paths.xml
<external-path name="external_images" path="Pictures/" />
I think I've done correctly, all things work except runtime exception in FileProvider.GetUriForFile() line.
Here's call trace.
enter image description here