在Android中向图库添加图像
我有一个应用程序可以做两件事:在Android中向图库添加图像,android,image,broadcast,Android,Image,Broadcast,我有一个应用程序可以做两件事: 它通过摄像头应用程序捕捉照片并将其保存到gallery中,这样就可以了 从图像中生成.gif并将其保存在ExternalStoragePublicDirectory-但这不起作用 这是我用于将文件添加到库中的方法: public static void galleryAddPic(Context context, String currentPhotoPath) { Intent mediaScanIntent = new Intent(Intent.
- 它通过摄像头应用程序捕捉照片并将其保存到gallery中,这样就可以了
- 从图像中生成.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上不起作用,有人能证实吗?
有没有比我的上述方法更好的解决方案来添加图片到图库?
干杯请尝试下面的代码
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();
}
}
“从图像中生成.gif并将其保存在ExternalStoragePublicDirectory中-但这不起作用”你是什么意思?您的问题是存储.gif文件还是将其添加到图库?很抱歉没有说清楚。我的意思是,我正在从应用程序(存储在SD卡中)拍摄所有照片,并从中生成.gif。然后我将这个.gif文件保存到SD卡文件夹“图片”中。我应该在Windows资源管理器和Gallery中看到它。奇怪的是,现在当我测试它时,它在Windows资源管理器中工作并显示。它有时工作,有时不工作。您应该查看一下
MediaScanner
类,文件不会立即可见。看看这个: