2014-09-11 149 views
0

我有做两件事情的应用程序:将图片添加到库中的Android

  • 它捕获通过相机应用的照片并将其保存到画廊 - 这一点也适用
  • 产生.gif注意出图像并保存它ExternalStoragePublicDirectory - 这不起作用

这是我使用的文件添加到库中的方法:

public static void galleryAddPic(Context context, String currentPhotoPath) { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(currentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    context.sendBroadcast(mediaScanIntent); 
} 

我已经阅读过某些地方,这不适用于Android Api 19,任何人都可以确认吗? 有没有比上述方法更好的将图片添加到图库的解决方案? 干杯

+0

“产生.gif注意出图像,并在ExternalStoragePublicDirectory保存它 - 这行不通”你是什么意思?您的问题是存储.gif或将其添加到图库? – Copernic 2014-09-11 10:12:20

+0

对不起,没有说清楚。我的意思是我从应用程序的所有图片(存储在SD卡中)并生成.gif。然后我将这个.gif保存到SD卡文件夹“图片”。我应该在Windows资源管理器和图库中看到它。 – 2014-09-11 10:15:20

+0

奇怪的是,现在当我测试它的作品并在Windows资源管理器中显示。它有时工作,有时不工作。 – 2014-09-11 10:16:04

回答

1

试试下面的代码对于

private void SaveImage(Bitmap finalBitmap) { 

    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_images");  
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
}