1

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

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alnov Lucky
  • 35
  • 1
  • 9

3 Answers3

1

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)
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
  • How do you save the image? Because this looks like a problem of taking the picture in landscape or portrait mode or simply reading wrongly from the image byte buffer? Can you add that section of code to your question. – Radu Ionescu Mar 17 '16 at 10:47
  • heres the full code bro, i cant post it in here because its overword https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java – Alnov Lucky Mar 18 '16 at 02:18
  • btw i can use this for saving it to the gallery.. getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mFile))); but the delay is so long its nearly 1 minute – Alnov Lucky Mar 18 '16 at 09:09
0

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);
                }
            });
Shahar
  • 3,692
  • 4
  • 25
  • 47
0

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

    }
};
Ashky
  • 62
  • 2
  • I know this is a very late comment, but do you know whether there is an alternative to SimpleDateFormat for devices using API 21? I get the following error: 'Call requires API level 24 (current min is 21): android.icu.text.SimpleDateFormat#SimpleDateFormat' – nxheller Feb 07 '17 at 22:23
  • @Zed check the marked answer from this question http://stackoverflow.com/questions/39055963/simpledateformat-gives-api-error – Ashky Feb 08 '17 at 16:27
  • for whatever reason, it does not appear in all gallery apps; what can I do about it? – VollNoob Apr 06 '18 at 14:36
  • found a solution. check this out: https://stackoverflow.com/questions/8667623/updating-gallery-after-taking-pictures-gallery-not-showing-all-the-pictures-andr/8667784#8667784 – VollNoob Apr 09 '18 at 11:27