【问题标题】:Saving image in Gallery在图库中保存图像
【发布时间】:2016-07-06 10:18:46
【问题描述】:

我正在截取屏幕截图并使用我的应用程序名称将其保存在存储中。它没有给出任何错误并将照片保存在存储中,但在我重新启动手机后它会出现在图库中。

我的照片保存方法如下:

public void saveQuoteImage(Bitmap quoteImage){
  Date now = new Date();
  android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
  try {
    // image naming and path  to include sd card  appending name you choose for file

    String dirPath=Environment.getExternalStorageDirectory().toString()+File.separator+"quotes_king";

    File dirFile=new File(dirPath);
    if(!dirFile.exists())dirFile.mkdirs();

    // String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
    // create bitmap screen capture
    File imageFile = new File(dirFile.getPath()+File.separator+ now + ".jpg");

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    quoteImage.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);

    outputStream.flush();
    outputStream.close();

  } catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
  }
}

我缺少什么?

谢谢

【问题讨论】:

  • 错误是什么?
  • @victorsosa 感谢您的评论。它没有给出任何错误,但仅在重新启动我的手机后才显示保存的图像。

标签: java android


【解决方案1】:

您只需在 MediaScanner 告知您的文件存在。

类似这样的:

public final void addManually(File file) {
        try{
            MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
                    file.getAbsolutePath(), file.getName(), null);
            mContext.sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.fromFile(file))
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

【讨论】:

    【解决方案2】:

    您应该在新文件上调用媒体扫描程序。

    how to run media scanner in android

    【讨论】:

      【解决方案3】:

      在android中使用媒体扫描器。只需添加下面的代码。

      MediaScannerConnection.scanFile(this, new String[] { imagefile.toString() }, null, null);
      

      【讨论】:

        猜你喜欢
        • 2017-05-26
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多