2011-08-14 3 views
0

私は描画APIの例:を試していて、図面を保存したいと思っています。以前の回答を見て、解決策を試しましたが、保存できません。Android fingerpaint api save

私は保存するために使用していたコードは、以下のコードでエンボスボタン用のAPIのコードを交換した

:私がクリックしたときに、私は、SDカードに直接画像を保存しようとしています

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カードの下にファイルを保存doesnt、誰もこれが動作しない理由を知っている?

また、私は許可を追加しました:事前に

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

おかげ

おかげ

編集:私は私が持っている、問題は権限またはそれがファイルを作成されている方法であると思いますがフォルダも作成しようとしましたが、どちらもそうではありません。他の権限が必要ですか?

+0

私は助けることができなかった申し訳ありません...とにかくあなたの助けMByDため – MByD

+0

おかげで、私も同じ問題を抱えています – James

+0

をしようとし続けます。しかし、あなたが言ったように、エラーは起きていません。しかし、私がSaveボタンをクリックしたとき、私は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(); 

My Screen Draw XMLファイル。最初の線形レイアウトは背景として機能し、2番目の線形レイアウトは描画パッドとして機能します。

<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

@Jamesは上記のコードを一見持っています。 – Raghunandan