如何在Android画廊中显示从应用程序拍摄的图像?

时间:2014-12-07 10:04:15

标签: android android-gallery

我想在手机图库中显示我的应用中相机拍摄的图像。图像会保存到文件夹中,但不会显示在图库中?

这是我的代码,但这似乎不起作用。

 getApplicationContext().getDir(
              getResources().getString(R.string.app_name), MODE_PRIVATE);

          fileUri = Uri.fromFile(new File((Environment.getExternalStorageDirectory() +
                  "/" +getResources().getString(R.string.app_name)),new Date().getTime() + ".jpg"));
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
          startActivityForResult(takePictureIntent,TAKE_PICTURE);
  }

       public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {

                  try {
                      GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
                      bitmap = getImageThumbnail.getThumbnail(fileUri, this);
                  } catch (FileNotFoundException e1) {
                      e1.printStackTrace();
                  } catch (IOException e1) {
                      e1.printStackTrace();
                  }

       protected void mediaScan() {
            getApplicationContext().sendBroadcast(
                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
                            Uri.parse(fileUri.toString())));
        }

有什么建议吗? 感谢

1 个答案:

答案 0 :(得分:1)

您应该将图片保存在Picture公共目录中,并在onActivityResult中调用mediaScan()