如何在绘图后保存图像

时间:2014-03-28 21:43:59

标签: android android-drawable

我正在使用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      

我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

尝试消除此行

  drawView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

答案 1 :(得分: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();
}