Android custom camera photos not shown in gallery - android

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);
}
});

Related

Camera.PictureCallback not working in nougat

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)

The code does not store image from layout in gallery

I am working on app that store its image from resource to gallery. with the below code, it does not give an error but do not work. help me through this.
RelativeLayout fulllayout;
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/ZXMCO Images");
fulllayout = (RelativeLayout) findViewById(R.id.layout);
public void save()
{
fulllayout.setDrawingCacheEnabled(true);
fulllayout.buildDrawingCache();
Bitmap localBitmap = fulllayout.getDrawingCache();
saveToInternalStorage(localBitmap);
}
private void saveToInternalStorage(Bitmap bitmapImage){
File pictureFile = getOutputMediaFile();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmapImage.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());
}
}
private File getOutputMediaFile() {
if (! root.exists()){
root.mkdirs();
}
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(root.getPath() + File.separator + mImageName);
return mediaFile;
}
It just show nothing. run without an error but could not find the image in gallery.
I Think the problem here is the Gallery didn't know that theres new images is stored on the device so we will need to notify it
There's two way to do that
Pre - Kitkat
using Boradcast
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Kitkat
You Can user the MediaScannerConnection to scan the new files
MediaScannerConnection.scanFile(context, new String[]{imageURL}, null,implment Listener Here);

How to save bitmap as PNG to a specific folder in the Gallery?

Given a bitmap that I want to save to a special folder in the public Gallery on the phone, I tried this:
public static void saveToGallery(Context context, Bitmap bitmap) {
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), File.separator + "FolderForMyApp");
path.mkdirs();
File imageFile = new File(path, "image_name.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (IOException e) {
Log.w(TAG, "Could not resolve file stream: " + e);
}
}
However it does not actually show up in the Gallery. What am I doing wrong?
It does not appear on the gallery because you have to refresh the gallery, so it knows about the new file
// Tell the media scanner about the new file so that it is
// immediately available to the user.
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);
}
});
Sry about the formatting. I'm on my phone.

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;
}

Categories

Resources