android中与摄像头相关的应用
在我的相机应用程序中,我有图像按钮、图像视图和两个按钮作为确认和保存。在第一次显示时,图像按钮是可见的,其他按钮是不可见的,通过单击图像按钮完成了如何打开相机的编码,也在图像视图中获得了图像,单击确认按钮后,会显示保存,但是在这个点击保存中,我想将在图像视图中捕获的图像保存在SD卡上的一个特定文件中,使用序列命名循环,就像在数字cam中使用扩展名为“.PNG”的方式一样。请帮助我。并提前感谢。首先您必须从捕获的图像返回位图,然后在点击按钮时使用此方法。我正在使用它android中与摄像头相关的应用,android,Android,在我的相机应用程序中,我有图像按钮、图像视图和两个按钮作为确认和保存。在第一次显示时,图像按钮是可见的,其他按钮是不可见的,通过单击图像按钮完成了如何打开相机的编码,也在图像视图中获得了图像,单击确认按钮后,会显示保存,但是在这个点击保存中,我想将在图像视图中捕获的图像保存在SD卡上的一个特定文件中,使用序列命名循环,就像在数字cam中使用扩展名为“.PNG”的方式一样。请帮助我。并提前感谢。首先您必须从捕获的图像返回位图,然后在点击按钮时使用此方法。我正在使用它 void Save()
void Save() {
if (null != view.getDrawable()) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
save = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
} else {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_LONG);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
view.setDrawingCacheEnabled(false);
}
在这里,我将从视图中获取图像作为现金,并将其保存为PNG
您需要做的是删除if语句
,然后将保存更改为您的位图名称
save.compress(bitmap.CompressFormat.PNG,100,out);`
它将是什么
void Save() {
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out); \\ change save to your Bitmap name
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
}
`这是我的文件夹字符串
String folder = "/sdcard/Pictures/StyleMe";
我的静态文件;
和P.S此方法将自动通知多媒体资料扫描新文件,无需重新启动手机或手动操作。如果答案是你想要的,一定要接受,并投下肯定的一票。
编辑:将其添加到清单
嘿,我感谢您的积极帮助,但有一个问题确实出现了,我不知道为什么我在emulator上使用此代码,并且也设置了SD卡,但仍然出现您在catch语句中编写的toast msg,我想是因为该方法没有从emulator获取SD卡。试着把它安装在你的手机上,确保它能正常工作。另外,我在我的应用程序上使用了这种方法,而且效果非常好,在xD之前我从未在emulator上尝试过。我在手机上测试了我的所有应用程序:Dmail-ur-emailid我将向你发送我的代码,因为我无法将此作为字数限制发布