Photo does not show up to gallery

我怕爱的太早我们不能终老 提交于 2019-12-18 09:41:38

问题


I'm trying to take a photo using the android camera and telling it to save to the phone's gallery. I think i messed up on the path, but i can't seem to find my error. Could someone help me? I'm very novice at android.

Code to call camera

   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  String uriToFileInExternalStorage = null;
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriToFileInExternalStorage);
                    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

code to handle photo and tell it to go to gallery.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                //check if camera has taken picture by checking request code

                Toast.makeText(MainActivity.this, "Photo Captured", Toast.LENGTH_SHORT).show();


                Uri mPath=data.getData();
                 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

                    mediaScanIntent.setData(mPath);
                    this.sendBroadcast(mediaScanIntent);

            }
        }

回答1:


Try code given here: Add the Photo to a Gallery

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);

I would suggest you to download example PhotoIntentActivity to read and understand how to fetch mCurrentPhotoPath value.




回答2:


This will work in all android versions used in on activity result of camera capture

MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);




回答3:


You need to set a value to this uriToFileInExternalStorage

Sample code:

fileName = "image_" + String.valueOf(numImages) + ".jpg";

File output = new File(direct + File.separator + fileName); // create
                                                                    // output
while (output.exists()) { // while the file exists
    numImages++; // increment number of images
    fileName = "image_" + String.valueOf(numImages) + ".jpg";
    output = new File(outputFolder, fileName);
}

uriToFileInExternalStorage = Uri.fromFile(output); 


来源:https://stackoverflow.com/questions/19558188/photo-does-not-show-up-to-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!