Android:重复捕获相同的图像

时间:2016-02-23 13:16:46

标签: java android

captureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String folder = "Capture";
                try{
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
                    Date currentTime = new Date();
                    String dateString = formatter.format(currentTime);
                    File sdCardPath = Environment.getExternalStorageDirectory();
                    File dirs = new File(Environment.getExternalStorageDirectory(), folder);

                    if (!dirs.exists()) {
                        dirs.mkdirs();
                    }
                    container.buildDrawingCache();
                    Bitmap captureView = container.getDrawingCache();
                    FileOutputStream fos;
                    String save;
                    try {
                        save = sdCardPath.getPath() + "/" + folder + "/" + dateString + ".jpg";
                        fos = new FileOutputStream(save);
                        captureView.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.close();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                Uri.parse("file://" + sdCardPath.getPath() + "/" + folder + "/" + dateString + ".jpg")));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(getApplicationContext(), dateString + ".jpg 저장", Toast.LENGTH_LONG).show();
                    recentImageName = "file://" + sdCardPath.getPath() + "/" + folder + "/" + dateString + ".jpg";
                } catch (Exception e) {
                }
            }
        });

我的应用程序有一些EditText。当我首先在EditText中放入一些单词并通过按钮捕获时,捕获按钮效果很好。 但是当我删除并在EditText中添加新单词并点击按钮时,结果与第一次捕获相同。

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

每次拍摄新照片都需要做以下事情:

每次调用view.getDrawingCache()之后每次调用位图缓存和view.setDrawingCacheEnabled(false)之前调用view.setDrawingCacheEnabled(true)。

示例:

view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
File imageFile = new File(Environment.getExternalStorageDirectory(),
        "Pictures/image.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
view.getDrawingCache(true).compress(CompressFormat.JPEG, 100,
        fileOutputStream);
fileOutputStream.close();
imageView.setDrawingCacheEnabled(false);

获得位图后需要setDrawingCacheEnabled(false)。

答案 1 :(得分:0)

您应该在

之后立即致电container.destroyDrawingCache()
container.buildDrawingCache();
Bitmap captureView = container.getDrawingCache();
container.destroyDrawingCache(); //<----