2014-03-28 126 views
0

我正在使用this教程在android中創建繪圖應用程序。如何在繪製後保存圖像

我想要做的是從圖庫中獲取圖片,然後在圖片上繪圖,試圖保存圖片。

繪圖後,當我嘗試保存圖像時,它只保存具有黑色背景的繪圖。在保存的圖像中看不到圖庫中的圖像。

我的代碼:

XML佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFCCCCCC" 
android:orientation="vertical" 
tools:context=".ImageActivity" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:orientation="horizontal" > 
    <ImageButton 
     android:id="@+id/my_save_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:contentDescription="@string/save" 
     android:src="@drawable/save" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:id="@+id/my_view_drawing_pad1" 
    android:layout_marginBottom="3dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="3dp" 
    android:layout_weight="1" > 

    <LinearLayout 
     android:id="@+id/my_view_drawing_pad" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 
    </LinearLayout> 
</LinearLayout> 
</LinearLayout> 

ImageActivity類:

public class ImageActivity extends Activity implements OnClickListener 
{ 
    private DrawingView drawView; 
    private ImageButton ibsaveBtn; 
    LinearLayout llDrawingPad; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_image); 
    drawView = new DrawingView(this); 
    llDrawingPad = (LinearLayout) findViewById(R.id.my_view_drawing_pad); 
    ibsaveBtn = (ImageButton)findViewById(R.id.my_save_btn); 
    ibsaveBtn.setOnClickListener(this); 

    // code to get image from gallery ... 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
    // getting the image 

    File file = new File(s); 
    if (file.exists()) { 

     fp = file.getAbsolutePath(); 
     d = Drawable.createFromPath(file.getAbsolutePath()); 

     drawView = new DrawingView(this); 
     llDrawingPad = (LinearLayout) findViewById(R.id.my_view_drawing_pad); 
     llDrawingPad.addView(drawView); 
     llDrawingPad.setBackgroundDrawable(d); 
     } 
    } // end onActivityResult 

    @Override 
    public void onClick(View view) 
    { 
     drawView.setDrawingCacheEnabled(true); 
     drawView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 

     String imgSaved = MediaStore.Images.Media.insertImage(
      getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID() 
      .toString() + ".png", "drawing"); 

     if (imgSaved != null) 
     { 
     Toast savedToast = Toast.makeText(getApplicationContext(), 
       "Drawing saved to Gallery!", Toast.LENGTH_SHORT); 
     savedToast.show(); 
     } else { 
       Toast unsavedToast = Toast.makeText(getApplicationContext(), 
       "Oops! Image could not be saved.", Toast.LENGTH_SHORT); 

       unsavedToast.show(); 
      } 
     drawView.destroyDrawingCache(); 
    } // end onClick  

缺少什麼我在這裏?

回答

0

試圖消除這一行

drawView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 
+0

想這一點,但仍然有同樣的問題。 –

0

嘗試在你的onClick這樣做:

drawView = new DrawingView(this); 
drawView.setDrawingCacheEnabled(true); 
Bitmap bitmap = drawView.getDrawingCache(); 
String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
File file = new File(path+"image.png"); 
FileOutputStream ostream; 
try { 
     file.createNewFile(); 
     ostream = new FileOutputStream(file); 
     bitmap.compress(CompressFormat.PNG, 100, ostream); 
     ostream.flush(); 
     ostream.close(); 

     Toast.makeText(getApplicationContext(), "image saved", 5000).show(); 
    } 
catch (Exception e) 
{ 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "error", 5000).show(); 
} 
+0

感謝您的幫助,但是這給了我一個例外: ** java.io.IOException:打開失敗:EROFS(只讀文件系統)** **導致:libcore.io.ErrnoException:打開失敗:EROFS(只讀文件系統)** –