Reputation: 35
I have a camera project and im using camera2 API
especially from this link
https://github.com/googlesamples/android-Camera2Basic
I can save the picture taken to my file manager located at DCIM/camera
for example, but when I open my gallery, it wont show my last picture.
Can anyone help me?
One more question, I want to make my camera can be a list: for example, when I open "LINE" and I want take picture with camera, I want my camera appears and can be chosen.
This is the sample code I tried for saving picture to the custom path:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFile = new File("/storage/emulated/0/DCIM/Camera", "pic.jpg");
int counter=1;
while (mFile.exists()) {
mFile = new File("/storage/emulated/0/DCIM/Camera", "pic" + String.format("%02d", counter) + ".jpg");
counter++;
}
}
Upvotes: 1
Views: 4262
Reputation: 62
To show the images in the gallery you need to add the ContentValues, in the example: https://github.com/googlesamples/android-Camera2Basic found the ImageReader.OnImageAvailableListener. This is my code:
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
// First I get the path to gallery and crate new Album to my app
String pathD = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/";
File mediaStorageDir = new File(pathD, "MyAlbum");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
}
}
/*Second I cut mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
from onActivityCreated and add here with the new path from my Album*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
mFile = new File(mediaStorageDir,"ImageName"+"_"+ timeStamp+".jpeg");
//Then the contentValues
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "ImageName");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.ORIENTATION, ORIENTATIONS.get(rotation));
values.put(MediaStore.Images.Media.CONTENT_TYPE,"image/jpeg");
values.put("_data", mFile.getAbsolutePath());
ContentResolver cr = getActivity().getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//This line is already in the code
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile));
}
};
Upvotes: 0
Reputation: 3692
If you want to add a single file to the gallery, try using this:
MediaScannerConnection.scanFile(context,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener()
{
public void onScanCompleted(String path, Uri uri)
{
Log.d("onScanCompleted", "Scanned " + path + " and uri " + uri);
}
});
Upvotes: 0
Reputation: 3532
Android media gallery might not detect your file immediately when you write it, but only later during a scan. To force it do do a scan you can use this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(mFile));
mContext.sendBroadcast(mediaScanIntent);
} else {
mContext.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
Since running a scan on every added file is a costly operation you can use this solution to manually add it
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, mFile);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
Upvotes: 1