2012-06-18 45 views
1

我用ImageView制作了一个应用程序,它显示用户绘制的位图,一切都很顺利,但现在我想给用户提供将图像从ImageView保存到jpeg文件的可能性,当我点击一个按钮。 我该怎么做?如何在Android中将ImageView的内容保存到位图?

这里是我的代码:

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的内容? (而以位图格式,然后其他的事情)

回答

0

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

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

} 
0

的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 
       } 
      } 

     } 
    } 
0

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(); 
    } 
相关问题