2012-06-18 82 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(); 
    } 
相關問題