Question

My app uses the phone camera to take pictures and save them in a specific folder. I can't see them with Android Gallery or plugging into my pc, but I can using a file manager app. I found a solution to this: I rename pictures with a file manager app and the I can see them in the gallery.

The code I'm using is:

Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String dirName = Environment.getExternalStorageDirectory()+"/MyAPP/APP"+ n +".jpg";
Uri uriSavedImage = Uri.fromFile(new File(dirName));
camera.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);
startActivityForResult(camera, 1);
n++;

AndroidManifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
Was it helpful?

Solution 2

I just needed to add some code to scan the picture and add it to the gallery

private void addGallery() {
    Intent mediaScanIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String currentPath = Environment.getExternalStorageDirectory()
            + "/MyAPP/APP" + n + ".jpg";
    File f = new File(currentPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

I added it in onActivityResult and it works

OTHER TIPS

for save image to specific folder,

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

if (!direct.exists()) {
    File wallpaperDirectory = new File("/sdcard/DirName/");
    wallpaperDirectory.mkdirs();
  }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

try this for accessing that image

private void showImage(String imageName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/SlamImages");

    if (direct.exists()) {

        File f = new File(Environment.getExternalStorageDirectory() + "/SlamImages/" + imageName);

        if (f.exists() && !imageName.equals("")) {
            Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
            imgUserLogo.setImageBitmap(bmp);
        } else {
            imgUserLogo.setImageResource(R.drawable.friend_image);
            Toast.makeText(FriendsDetailsActivity.this, "Image Does not exist", Toast.LENGTH_SHORT).show();
        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top