Camera.PictureCallback not working in nougat - android

I am working in application in which I have to capture image from camera and save that image inside folder, Right now my code is working fine upto marshmallow but when testing in nougat, I am unable to save as well as unable to create folder. I have also given all the permission. Please tell me what I am doing wrong
This method is not working in naught
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d("", "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
mCamera.stopPreview();
} catch (FileNotFoundException e) {
Log.d("", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("", "Error accessing file: " + e.getMessage());
}
}
};
This is my code for create and saving the image file
private File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Carco");
// 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()){
Log.d("Carco", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
///storage/emulated/0/Pictures/Carco
if (type == MEDIA_TYPE_IMAGE){
String img_name = "IMG_"+ timeStamp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
img_name);
imgPath = mediaStorageDir.getPath()+"/"+img_name;
Log.e("Path: ",mediaStorageDir.getPath()+"/"+img_name);
} else {
return null;
}
return mediaFile;
}

Google for runtime permissions.
You are #1237 with this problem this year.
/storage/emulated/0 IS BLANK. (using nbsp-team/ Material File Picker)

Related

how to save my QR generated image into gallery android?

Here, I am generating Qr code as an image view. I want to save that image file into phone gallery. Please, help me.
first of all create bitmap from imageview using following code
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
now save this bitmap in internal storage
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
/** 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="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}

How to upload on Dropbox Camera Capture image with Watermark without Compress (Keep Orignal Quality) in Android

I am using this code right now but its destroy image quality.
I have issue regarding blur image
if (requestCode == IMAGE_REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// kkkk(data.getData());
picture = (Bitmap) data.getExtras().get("data");//this
// I pass bitmap to function to get watermark
 result = mark(picture , sh.getString("BARCODE", null), 10, 50,R.color.colorPrimaryDark, 200, 30, false);
// now i need a file to upload image on dropbox
file = storeImage(result);
}
// here is my function
private File storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d("",
"Error creating media file, check storage permissions: ");// e.getMessage());
return null;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG,55, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.d("", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("", "Error accessing file: " + e.getMessage());
}
return pictureFile;
}
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_HHmmss").format(new Date());
File mediaFile;
increase++;
String mImageName = sh.getString("BARCODE", null) + "_" + timeStamp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
</i>
}

How to automatically crop image after it is taken by custom camera app

I've made custom camera app, which works great but I'm stuck trying to get square images. For few days now I'm trying to make preview squared but now I'm thinking that is wrong approach especially when I read somewhere that Instagram app makes square images by hiding portion of preview with layout section which holds camera buttons (thus making preview look like it is square) and then cropping an image after it is taken. I have an idea how would I do first part but how would I crop image after its taken?
Here are my two methods which capture and save image:
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d("Camera error",
"Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("Camera error", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("Camera error", "Error accessing file: " + e.getMessage());
}
Intent intent = new Intent(MyCamera.this, NewItem.class);
intent.putExtra("imagePath", pictureFile.getAbsolutePath());
startActivity(intent);
}
};
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Pickante");
// 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("yyyyMMdd_HHmmss")
.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;
}

Insert Pictures from camera to SQLite in Android

So here is my question... I've been working on a SQLite Data Base that stores some geolocation data and I'd like insert images from the camera. I've read is better insert the image path instead of the image, but I can't find the way to get the image path.
This is what I've got already.
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
public File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp"
);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
Thanks!
file.getAbsolutePath();
that simple :)
So in context it might look something like this
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
saveFileLocation(pictureFile.getAbsolutePath);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
where saveFileLocation is a function you make that takes the path string and saves it to your sql db.
you can do mediaFile.getAbsolutePath() to get the path that you want to store in the database.

Android custom camera photos not shown in gallery

I'm creating custom android camera layout.
I've used information from http://developer.android.com/guide/topics/media/camera.html#basics. Everithing works fine except one thing.
The photo is not shown in the phone's gallery. The metod is:
private Camera.PictureCallback cameraCallback = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.flush();
fos.close();
createPhotoAndShowPreview(Uri.fromFile(pictureFile));
} catch (FileNotFoundException e) {
Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(Values.APPLICATION_TAG + ACTIVITY_TAG, "Error accessing file: " + e.getMessage());
}
}
};
private static File getOutputMediaFile(){
File galleryDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (!galleryDir.exists()) {
if (!galleryDir.mkdir())
{
Log.d(Values.APPLICATION_TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(galleryDir.getPath() + File.separator +
"gfranq_"+ timeStamp + ".jpg");
return mediaFile;
}
Method createPhotoAndShowPreview(Uri.fromFile(pictureFile)); works fine, the file exists during runtime and I can use it, but when I exit the app there is no file in the gallery. I used a file manager to check if the file is deleted, but its not deleted, it exists after exiting the application, but the system doesn't recognize that it is an image.
Update: The photos appered in the gallery after I connected my phone to my pc. But new photos still don't appear in the gallery untill I connect and disconnect my phone to pc. rebooting the phone has the same effect.
You need to send out a broadcast:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});

Categories

Resources