2014-03-28 3 views
0

나는 안드로이드에 그림 응용 프로그램을 만들기위한 this 자습서를 사용하고 있습니다.그림을 그린 후 이미지를 저장하는 방법

내가하려는 것은 갤러리에서 이미지를 얻은 다음 이미지를 그린 후 저장하는 것입니다.

그림을 저장 한 후에 이미지를 저장하려고하면 도면이 검은 색으로 저장됩니다. 갤러리에서 가져온 이미지는 저장된 이미지에 표시되지 않습니다.

내 코드 :

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 : open failed : EROFS (읽기 전용 파일 시스템) ** –

관련 문제