I am calling this method when capturing image from the nought.
private void CallCameraFeature() {
Intent cameraOpeningIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String fileName = EmpConstants.startImgName +
new SimpleDateFormat(EmpConstants.PhotoFileFormat,
Locale.US).format(new Date());
File imgFile = new File(mContext.getFilesDir(), "images");
File outFile = new File(imgFile, fileName + ".jpg");
Uri photoURi = FileProvider.getUriForFile(mContext,
BuildConfig.APPLICATION_ID + ".provider", outFile);
cameraOpeningIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURi);
startActivityForResult(cameraOpeningIntent, REQUEST_IMAGE_CAPTURE);
}
}
I have created xml file in
values -> provider_paths.xml
Storing the image in this path
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths >
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>
Path defined like this to store image DCIM.
public String getEmpThumbImageDirPath() {
try {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_
DCIM).toString() + EmpConstants.appDir;
}catch (Exception e) {
Log.d("eEmp/ImgDir", e.toString());
return "";
}
camera is opening and capturing the image but image is not loading. What mistake I have done.
Any help would be appreciated.
04-13 20:05:43.738 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: directory was created successfully.
04-13 20:05:43.739 30272-30272/com.efftronics.android.eEmployee E/ContentValues: run: image folder path is: /storage/emulated/0/FolderName/InsideFolderNameIFYOUWant
04-13 20:05:43.739 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: image file name is: imageName_1523630143739
04-13 20:05:49.791 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: directory already exists.
04-13 20:05:49.792 30272-30272/com.efftronics.android.eEmployee E/ContentValues: run: image folder path is: /storage/emulated/0/FolderName/InsideFolderNameIFYOUWant
04-13 20:05:49.792 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: image file name is: imageName_1523630149792
Try this, It works for me:
private void openCamera()
{
try
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
try
{
// Create the File where the photo should go
File photoFile = createImageFile();
// 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, CAPTURE_IMAGE_REQUEST_CODE);
}
else
{
Log.e(TAG, "openCamera: image was not captured.");
}
}
catch (Exception ex)
{
// Eor occurred while creating the File
ex.printStackTrace();
}
}
}
catch (Exception e)
{
Log.e(TAG, "openCamera: exception while opening camera:");
e.printStackTrace();
}
}
private String mCurrentPhotoPath;
private File createImageFile()
{
// Create an image file name
File sd = Environment.getExternalStorageDirectory();
File imageFolder = new File(sd.getAbsolutePath() + File.separator +
"FolderName" + File.separator + "InsideFolderNameIFYOUWant");
if (!imageFolder.exists())
{
if (imageFolder.mkdirs())
{
Log.e(TAG, "createImageFile: directory was created successfully.");
}
else
{
Log.e(TAG, "createImageFile: directory was not created.");
}
}
else
{
Log.e(TAG, "createImageFile: directory already exists.");
}
Log.e(TAG, "run: image folder path is: " + imageFolder.getAbsolutePath());
File image = null;
File mediaFile = new File(imageFolder + File.separator );
String imageFileName = "imageName_" + System.currentTimeMillis();
Log.e(TAG, "createImageFile: image file name is: " + imageFileName);
try
{
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
mediaFile /* directory */);
}
catch (IOException e)
{
Log.e(TAG, "createImageFile: exception occurred while creating image file:\n");
e.printStackTrace();
}
if (image != null)
{
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
else
{
Log.e(TAG, "createImageFile: image was not created.");
return null;
}
}
Add this in your manifest file:
<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 and xml resource directory and add and file_paths xml file and inside that add this:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="PathOfYourFolder" />
</paths>
And in your on activity result method just use the mCurrentPhotoPath to load the image into your image view. Use Picasso to do that.
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 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");
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
My app has features to 'Attach File', 'Take Picture', 'Take Video' etc. I was passing a File Uri to a new Intent but am getting the FileUriExposedException in Nougat. Hence have modified the code to use FileProvider. I am getting the Content Uri fine, but I get java.io.FileNotFoundException when I try to read or upload the file/picture/video. Am I setting the permissions wrong? Or do I need to set permissions in another way?
Here is what I am doing:
Android_manifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="."/>
<external-path name="external_files" path="."/>
</paths>
MainActivity.java:
/*
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Config.IMAGE_DIRECTORY_NAME);
if (!mediaStorageDir.exists()) {
try {
mediaStorageDir.mkdirs();
Log.d(TAG, "Created directory " + mediaStorageDir.getPath());
} catch (SecurityException e) {
e.printStackTrace();
}
}
if (mediaStorageDir != null) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_1" + ".jpg");
try {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider", mediaFile);
return photoURI;
} catch (IllegalArgumentException e) {
Log.e("File Selector","The selected file can't be shared: " +mediaFile);
}
}
return null;
}
In shouldOverrideUrlLoading (where I am setting up the Intent):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.putExtra(Intent.EXTRA_MIME_TYPES,extraMimeTypes);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),PICKFILE_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "Please install a File Explorer.", Toast.LENGTH_SHORT).show();
}
In onActivityResult:
case PICKFILE_REQUEST_CODE:
if (resultCode == RESULT_OK){
Uri selectedFile = data.getData();
try {
filePath = getPath(this,selectedFile);
} catch (URISyntaxException e) {
e.printStackTrace();
}
System.out.println("PICKFILE_REQUEST " + filePath);
if(filePath != null){
new UploadFileToServer().execute();
}else{
Toast.makeText(getApplicationContext(), " Please select from File Explorer or Gallery ", Toast.LENGTH_SHORT).show();
}
}
In the Android Monitor my output shows
I/System.out: PICKFILE_REQUEST /storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg
which I guess means the content Uri is correct
However, I am unable to upload the file
java.io.FileNotFoundException: /storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg: open failed: EACCES (Permission denied)
Does this mean my permissions are not set correctly? How do I correct this.
Your code looks fine, but instead of getting the file URI through FileProvider, use the file's absolute path. Use your mediaFile and get absolute path by mediaFile.getAbsolutePath().
This will return a string which you can use to read the file.
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.