android中与相机相关的应用程序

时间:2013-02-20 07:08:16

标签: android

在我的相机应用程序中,我有图像按钮,图像视图和两个按钮作为确认和保存。在第一次显示时,图像按钮是可见的而其他是不可见的,完成了如何通过点击获取相机的编码图像按钮,并在图像视图和点击确认按钮时显示保存,但在此保存点击我想保存图像视图中捕获的图像在SD卡上的特定文件与串行命名循环用数字相机“.PNG”扩展。请帮助我。并提前感谢

1 个答案:

答案 0 :(得分:0)

首先你必须从捕获的图像中返回位图,然后在按钮上使用这个方法点击我使用它。

    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 statement并将保存更改为您的位图名称 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";

和我的static File file; 和P.S这种方法会自动告诉图库扫描新文件,而无需重新启动手机或手动操作。如果您寻求的是并确认投票,请接受答案。 修改:在您的清单<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

中添加此内容