【问题标题】:How can I save the content of an ImageView to a Bitmap in Android?如何将 ImageView 的内容保存到 Android 中的位图?
【发布时间】:2012-06-18 09:43:22
【问题描述】:

我已经制作了一个带有 ImageView 的应用程序,该应用程序显示用户绘制的位图,一切顺利,但现在我想让用户在单击时将图像从 ImageView 保存到 jpeg 文件在一个按钮上。 我该怎么做?

这是我的代码:

    ImageView imageview;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageview=(ImageView)findViewById(R.id.imageviewcomponent);
        imageview.setDrawingCacheEnabled(true);
    }

    //This method sets the Bitmap into the ImageView from the ActivityResult of the Drawing Activity
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == get_code && resultCode == RESULT_OK)
    {
        iv.setImageBitmap((Bitmap) data.getParcelableExtra("Painting"));
        }
    }

    //This is the onClick method of the button for saving the image
    public void SaveAsImage(View btsave) throws FileNotFoundException {
        Bitmap b=Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(),        Bitmap.Config.ARGB_8888);
        OutputStream fOut = null;
        new File("/sdcard/signatures").mkdir();
        fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
        b.compress(CompressFormat.JPEG, 95, fOut);
    }

创建文件夹和保存图片的代码是工作的,因为我可以在myPainting文件夹中找到一个image.jpg文件,但是图片的内容是全黑的,没有用户绘制的图片。 如何获取 ImageView 的内容? (而不是位图格式而不是其他东西)

【问题讨论】:

    标签: android bitmap save android-imageview


    【解决方案1】:

    试试这个代码,让我知道会发生什么..

    //This is the onClick method of the button for saving the image
        public void SaveAsImage(View btsave) throws FileNotFoundException {
    
            Bitmap b = Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(),        Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(b);
            imageview.draw(canvas);
            OutputStream fOut = null;
            new File("/sdcard/signatures").mkdir();
            fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
            b.compress(CompressFormat.JPEG, 95, fOut);
    
    }
    

    【讨论】:

      【解决方案2】:

      user370305 代码的略微修改版本也应该可以工作

      static void saveDrawable(ImageView imageView) throws FileNotFoundException{
              Drawable drawable = imageView.getDrawable();
              Rect bounds = drawable.getBounds();
              Bitmap bitmap = Bitmap.createBitmap(bounds.width(),bounds.height(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(bitmap);
              drawable.draw(canvas);
              OutputStream fOut = null;
              try {
                  new File("/sdcard/signatures").mkdir();
                  fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
              } finally {
                  if ( fOut != null ){
                      try {
                          fOut.close();
                      } catch (IOException e) {
                          //report error
                      }
                  }
      
              }
          }
      

      【讨论】:

        【解决方案3】:

        Imageview 内部使用 ScaleType。 它将根据屏幕的像素缩放您的图像,如果您想获得图像的原始质量,请使用绘图缓存,

          public void SaveAsImage(Holder imageview) throws FileNotFoundException {
        
            View view=findViewById(R.id.holder_frame);
            Bitmap bmp=viewToBitmap(imageview);
            imageview.setDrawingCacheEnabled(true);
            imageview.buildDrawingCache(true);
        
        
           Bitmap bitmap1=imageview.getDrawingCache();
            OutputStream fOut = null;
        
            new File("/sdcard/Pic").mkdir();
            fOut = new FileOutputStream("/sdcard/Pic/image4.jpg");
            bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        
            imageview.setDrawingCacheEnabled(false);
            imageview.destroyDrawingCache();
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-31
          • 2021-06-02
          • 1970-01-01
          • 1970-01-01
          • 2013-02-28
          • 2012-11-19
          相关资源
          最近更新 更多