【问题标题】:Android save images not update to galleryAndroid保存图像不更新到画廊
【发布时间】:2015-08-03 05:02:58
【问题描述】:

我正在尝试将我的图像从 ImageView 保存到 sd 卡,它们正在保存到 sd 卡,但它们没有更新到图库。这是我的代码。

public void save(View v) {
    bitmap = BitmapFactory.decodeResource(getResources(), backgrounds.get(currentPosition)) ;
    File filepath = Environment.getExternalStorageDirectory();
    File dir = new File(filepath.getAbsolutePath()
    + "/Folder/");
    dir.mkdirs();
    String Image = System.currentTimeMillis()+".Png";
    File file = new File(dir, Image);
    Toast.makeText(MainActivity.this, "Image Saved to SD Card",
    Toast.LENGTH_SHORT).show();
    try {
    output = new FileOutputStream(file);
    // Compress into png format image from 0% - 100%
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    output.flush();
    output.close();
   }
    catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

【问题讨论】:

标签: android bitmap imageview gallery


【解决方案1】:

你必须先给图片添加 mime 类型

它会起作用的。

MediaScannerConnection.scanFile(
    context, 
    new String[]{ pathToFile1, pathToFile2 }, 
    new String[]{ "audio/mp3", "*/*" }, 
    new MediaScannerConnectionClient()
    {
        public void onMediaScannerConnected()
        {
        }
        public void onScanCompleted(String path, Uri uri)
        {
        }
    });

【讨论】:

    【解决方案2】:

    一旦您将位图保存为 sdCard 中的 PNG 文件。 确保 Gallery 知道 PNG 文件已创建。

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多