2011-08-14 26 views
0

我一直在尝试绘制api示例:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html并希望保存绘图,我查看了以前的答案并尝试了解决方案,但无法将其保存。Android fingerpaint api保存

我已经更换了代码的API与我使用保存代码,下面的代码的浮雕按钮:

public boolean onOptionsItemSelected(MenuItem item) { 
    mPaint.setXfermode(null); 
    mPaint.setAlpha(0xFF); 

    switch (item.getItemId()) { 
     case COLOR_MENU_ID: 
      new ColorPickerDialog(this, this, mPaint.getColor()).show(); 
      return true; 
     case EMBOSS_MENU_ID: 


      String path = Environment.getExternalStorageDirectory().toString(); 
      OutputStream fOut = null; 
      File file = new File(path, "screentest.jpg"); 
     try { 
      fOut = new FileOutputStream(file); 
      mBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
      fOut.flush(); 
      fOut.close(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

我想直接将图像保存到SD卡,当我点击按钮保存,没有错误,但它不保存在SD卡下的文件,任何人有任何想法,为什么这是行不通的?

而且我已经添加了权限:提前

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

感谢

感谢

编辑:我认为这个问题是有权限或者是创建该文件的方式,我有试图创建一个文件夹,它也不会,我是否需要任何其他权限?

+0

抱歉,我不能帮... – MByD

+0

不管怎样,谢谢您的帮助MByD,会继续努力 – James

+0

我也有同样的问题。但正如你所说,没有错误出现。但是当我点击保存按钮时,我收到了ForceCLose错误。请帮助我。 – Akshay

回答

0

要保存绘图,请使用下面的代码。

View content = findViewById(R.id.rlid);// get the rootview of drawing screen 
    content.setDrawingCacheEnabled(true); //This is taking screen shot of your screen 

使用下面的代码名称保存它的你choice.I已经习惯了alertdialog它会提示用户输入他的选择将图像保存到画廊的名字。

AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this); 
    editalert.setTitle("Please Enter the name with which you want to Save"); 
    final EditText input = new EditText(DrawingRoomScreen.this); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.FILL_PARENT, 
         LinearLayout.LayoutParams.FILL_PARENT); 
input.setLayoutParams(lp); 
    editalert.setView(input); 
    editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         content.setDrawingCacheEnabled(true); 
         String name= input.getText().toString(); 
         Bitmap bitmap = content.getDrawingCache();// your drawing View 
       //File f= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();  
      //String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(); 
        String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
         File file = new File("/sdcard/"+name+".png");   
         try 
         { 
          if(!file.exists()) 
         { 
          file.createNewFile(); 
         } 
          FileOutputStream ostream = new FileOutputStream(file); 
          bitmap.compress(CompressFormat.PNG, 10, ostream); 

          ostream.close(); 
          content.invalidate();       
         } 
         catch (Exception e) 
         { 
          e.printStackTrace(); 
         }finally 
         { 

          content.setDrawingCacheEnabled(false);// don't want previous draw to be cached. every time user opts o save the draw the new draw will be cached.       
         } 
        } 
       }); 

       editalert.show(); 

我的屏幕绘制XML文件。第一个线性布局充当背景,第二个线性布局充当绘图板。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rlid" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:weightSum="1.0" > 

    <LinearLayout 
     android:id="@+id/view_drawing_pad" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </LinearLayout> 
</LinearLayout> 
</RelativeLayout> 
+0

@詹姆斯看看上面的代码。 – Raghunandan