Reputation: 6974
I have a problem with save photo, in app directory (like example whatsapp) when take a picture directly on my app.
In my Manifest I have set these permits:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="com.myapp.READ">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
In my activity I have:
public void showCamera(View v){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.myapp.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
galleryAddPic();
}
}
}
String mCurrentPhotoPath;
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
And in my filePath resource:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.myapp/files/Pictures" />
It open camera, it take a picture and return on my activity, but I can not find it anywhere, so seem that not save a photo.
Upvotes: 0
Views: 814
Reputation: 301
if everything working right then put galleryAddPic() method in onActivityResult where you collect the captured picture.
Upvotes: 1
Reputation: 1007494
I can not find it anywhere
You did not indicate how you are looking for it. Use adb shell
to get a filesystem-level look at what is on your device or emulator.
Right now, your ACTION_MEDIA_SCANNER_SCAN_FILE
will not work, as you are using an invalid path. A filesystem path does not begin with file:
. I recommend getting rid of mCurrentPhotoPath
entirely and hold onto the file returned by createImageFile()
instead, using that with your ACTION_MEDIA_SCANNER_SCAN_FILE
request (or, use MediaScannerConnection.scanFile()
instead of ACTION_MEDIA_SCANNER_SCAN_FILE
).
Also, as Hardik notes, you are calling galleryAddPic()
too soon. Wait until the picture is captured, by calling galleryAddPic()
in onActivityResult()
. While the file should exist at the point when you are calling galleryAddPic()
now, it will not have the correct details (e.g., file size). This may confuse the user. You are better off waiting until the picture is ready, then ask for it to be indexed.
Upvotes: 0