-1

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

4

1 回答 1

0

first you have to return bitmap from the image captured and then use this method on Button click i'm using it tho.

    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);
}

here i'm getting the image from the View as cashes and save it as PNG what you have to do is delete if statement and change save to your bitmap name save.compress(Bitmap.CompressFormat.PNG, 100, out);` and which it will be

    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();
        }

}

`
and here is my folder string

    String folder = "/sdcard/Pictures/StyleMe";

and my static File file; and P.S this method it will automatically tell the Gallery to scan for new files , without restarting the phone or doing it manually . do accept the answer if that what you seek for and vote it for sure. Edit: add this in your manifest <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

于 2013-02-20T07:46:04.497 回答