如何在android中清除位图?

时间:2014-07-04 06:30:38

标签: android email bitmap screenshot

您好我有一个应用程序来截取屏幕截图并发送到电子邮件。当我第二次截取屏幕截图并附加到电子邮件时,该电子邮件包含第一个屏幕截图。我认为位图没有清除。任何人都可以帮助我。我很抱歉我的英语很差。

这是我的代码;

email_icon1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Toast.makeText(getApplicationContext(), "email_icon clicked", Toast.LENGTH_SHORT).show();


                     View v1 = getWindow().getDecorView().getRootView();
                        // View v1 = iv.getRootView(); //even this works
                        // View v1 = findViewById(android.R.id.content); //this works too
                        // but gives only content
                     v1.setDrawingCacheEnabled(true);
                        myBitmap = v1.getDrawingCache();

                        saveBitmap(myBitmap);
                }
            });

public void saveBitmap(Bitmap bitmap) {
          String filePath = Environment.getExternalStorageDirectory()
                  + File.separator + "Pictures/screenshot.png";
          File imagePath = new File(filePath);
          FileOutputStream fos;
          try {
              fos = new FileOutputStream(imagePath);
              bitmap.compress(CompressFormat.PNG, 100, fos);
              fos.flush();
              fos.close();
              sendMail(filePath);
          } catch (FileNotFoundException e) {
              Log.e("GREC", e.getMessage(), e);
          } catch (IOException e) {
              Log.e("GREC", e.getMessage(), e);
          }
        }

        public void sendMail(String path) {


          Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
          emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                  new String[] { "athulya@extraslice.com" });
          emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                  "giMobile ScreenShot");
          emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                  "Sent from my AndroidTab");
          emailIntent.setType("image/png");
          Uri myUri = Uri.parse("file://" + path);
          emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
          startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }

先谢谢。

2 个答案:

答案 0 :(得分:0)

为了从视图中重新创建位图,请按照此顺序

    holder.setDrawingCacheEnabled(true);
    Bitmap bmp = holder.getDrawingCache();

然后在保存之后确保destroy view Caches并将其添加到save方法的末尾以完全销毁视图缓存并重新开始每次重新绘制视图单击保存方法或您正在使用的任何方法..

holder.setDrawingCacheEnabled(false);

答案 1 :(得分:0)

获取位图后执行此操作

  

v1.setDrawingCacheEnabled(真); myBitmap = v1.getDrawingCache();   v1.setDrawingCacheEnabled(假); saveBitmap(MYBITMAP);