简体   繁体   English

将图像保存到我的图库中

[英]Saving an image into my Gallery

I'm making an Android application in Android studio where the user can take a photo and save it into a new folder in their gallery. 我正在Android Studio中制作一个Android应用程序,用户可以在其中拍摄照片并将其保存到图库中的新文件夹中。 Currently the application takes the pictures fine but doesn't save the image into the new "SOC" folder in my gallery. 目前,该应用程序拍摄的照片很好,但不会将图像保存到我的图库中的新“SOC”文件夹中。 I'm not getting any errors and I have no idea why it's not saving the image. 我没有收到任何错误,也不知道为什么它没有保存图像。 Any help would be appreciated. 任何帮助,将不胜感激。

My code is as fallows 我的代码就像休耕一样

static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
public void onClickbtnCamera(View v)
{
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/SOC","QR_"+timeStamp+ ".png"));
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(imageIntent, 1);
}



   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {

            //Check for succesful result code
            if (resultCode == -1) {
                //Show your Toast when the result is a success.
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Picture is saved in your SOC gallery", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 100, 0);
                toast.show();
            }
        }
    }

I think you must manually add your new photo Uri to Media Content Provider. 我认为您必须手动将新照片Uri添加到媒体内容提供商。 take a look here 看看这里

Call this method in your Activity for result 在Activity中调用此方法以获得结果

 protected void addPhotoToGallery() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(getCurrentPhotoPath());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.getActivity().sendBroadcast(mediaScanIntent);
}

you should save the photo path before launching the intent and then getCurrentPhotoPath() must get that path 您应该在启动intent之前保存照片路径,然后getCurrentPhotoPath()必须获取该路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM