package com.android.demo.notepad3;
 
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class NoteEdit extends Activity {
 
 /////////////////////////////////////////////////////
 private static final int PICK_FROM_CAMERA = 0;
 private static final int PICK_FROM_ALBUM = 1;
 private static final int CROP_FROM_CAMERA = 2;
 private Uri mImageCaptureUri;
 private ImageView mPhotoImageView;
 private Button mButton;
 ///////////////////////////////////////////////////
 
 private EditText mTitleText;
    private EditText mBodyText;
    private Long mRowId;
    
    private NotesDbAdapter mDbHelper;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        
        setContentView(R.layout.note_edit);
   ////////////////////////////////////////////////////////////////////////     
  mButton = (Button) findViewById(R.id.button);
  mPhotoImageView = (ImageView) findViewById(R.id.image);
  mButton.setOnClickListener(ClickName);
  
   //////////////////////////////////////////////////////////////////////////////////   
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
      
        Button confirmButton = (Button) findViewById(R.id.confirm);
       
        mRowId = savedInstanceState != null ? 
          savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID) : null;
        
        if (mRowId == null) {
   Bundle extras = getIntent().getExtras();
   mRowId = extras != null ? 
     extras.getLong(NotesDbAdapter.KEY_ROWID) : null;
  }
          
        populateFields();
        
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
          
        });
               
    }
 
 private void populateFields() {
  if (mRowId != null) {
   Cursor note = mDbHelper.fetchNote(mRowId);
   startManagingCursor(note);
   mTitleText.setText(note.getString(
     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
   mBodyText.setText(note.getString(
     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
  }
 }
 @Override
 protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
 }
 
    @Override
 protected void onPause() {
  super.onPause();
  saveState();
 }
 private void saveState() {
  String title = mTitleText.getText().toString();
  String body = mBodyText.getText().toString();
  
  if (mRowId == null) {
   long id = mDbHelper.createNote(title, body);
   if (id > 0) {
    mRowId = id;
   }
  } else {
   mDbHelper.updateNote(mRowId, title, body);
  }
 }
 @Override
 protected void onResume() {
  super.onResume();
  populateFields();
 }
 
 /**
  * 카메라에서 이미지 가져오기
  */
 private void doTakePhotoAction()
 {
  /*
   * 참고 해볼곳
   * http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
   * http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image
   * http://www.damonkohler.com/2009/02/android-recipes.html
   * http://www.firstclown.us/tag/android/
   */
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  
  // 임시로 사용할 파일의 경로를 생성
  String url = "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
  mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), url));
  
  intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
  intent.putExtra("return-data", true);
  startActivityForResult(intent, PICK_FROM_CAMERA);
 }
 
 /**
  * 앨범에서 이미지 가져오기
  */
 private void doTakeAlbumAction()
 {
  // 앨범 호출
  Intent intent = new Intent(Intent.ACTION_PICK);
  intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
  startActivityForResult(intent, PICK_FROM_ALBUM);
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  if(resultCode != RESULT_OK)
  {
   return;
  }
  switch(requestCode)
  {
   case CROP_FROM_CAMERA:
   {
    // 크롭이 된 이후의 이미지를 넘겨 받습니다. 이미지뷰에 이미지를 보여준다거나 부가적인 작업 이후에
    // 임시 파일을 삭제합니다.
    final Bundle extras = data.getExtras();
 
    if(extras != null)
    {
     Bitmap photo = extras.getParcelable("data");
     mPhotoImageView.setImageBitmap(photo);
    }
 
    // 임시 파일 삭제
    File f = new File(mImageCaptureUri.getPath());
    if(f.exists())
    {
     f.delete();
    }
 
    break;
   }
 
   case PICK_FROM_ALBUM:
   {
    // 이후의 처리가 카메라와 같으므로 일단  break없이 진행합니다.
    // 실제 코드에서는 좀더 합리적인 방법을 선택하시기 바랍니다.
    
    mImageCaptureUri = data.getData();
   }
   
   case PICK_FROM_CAMERA:
   {
    // 이미지를 가져온 이후의 리사이즈할 이미지 크기를 결정합니다.
    // 이후에 이미지 크롭 어플리케이션을 호출하게 됩니다.
 
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(mImageCaptureUri, "image/*");
 
    intent.putExtra("outputX", 90);
    intent.putExtra("outputY", 90);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, CROP_FROM_CAMERA);
 
    break;
   }
  }
 }
 
 
 View.OnClickListener ClickName = new OnClickListener() {  
  
  //@Override
  public void onClick(View v)
  {
   DialogInterface.OnClickListener cameraListener = new DialogInterface.OnClickListener()
   {
    //@Override
    public void onClick(DialogInterface dialog, int which)
    {
     doTakePhotoAction();
    }
   };
   
   DialogInterface.OnClickListener albumListener = new DialogInterface.OnClickListener()
   {
    //@Override
    public void onClick(DialogInterface dialog, int which)
    {
     doTakeAlbumAction();
    }
   };
   
   DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
   {
    //@Override
    public void onClick(DialogInterface dialog, int which)
    {
     dialog.dismiss();
    }
   };
   
   new AlertDialog.Builder(NoteEdit.this)
    .setTitle("업로드할 이미지 선택")
    .setPositiveButton("사진촬영", cameraListener)
    .setNeutralButton("앨범선택", albumListener)
    .setNegativeButton("취소", cancelListener)
    .show();
  }
     
  }; 
 
}
 
 
 


3.JPG

 

2.JPG

 

11-30 02:51:56.034: W/KeyCharacterMap(332): No keyboard for id 0
11-30 02:51:56.045: W/KeyCharacterMap(332): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-30 02:52:02.915: D/AndroidRuntime(332): Shutting down VM
11-30 02:52:02.915: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-30 02:52:03.035: E/AndroidRuntime(332): FATAL EXCEPTION: main
11-30 02:52:03.035: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to pause activity {com.android.demo.notepad3/com.android.demo.notepad3.NoteEdit}: java.lang.NullPointerException
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2329)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2286)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2266)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.access$1700(ActivityThread.java:117)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3647)
11-30 02:52:03.035: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
11-30 02:52:03.035: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
11-30 02:52:03.035: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-30 02:52:03.035: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-30 02:52:03.035: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)
11-30 02:52:03.035: E/AndroidRuntime(332): Caused by: java.lang.NullPointerException
11-30 02:52:03.035: E/AndroidRuntime(332):  at com.android.demo.notepad3.NoteEdit.onSaveInstanceState(NoteEdit.java:100)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.Activity.performSaveInstanceState(Activity.java:1037)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
11-30 02:52:03.035: E/AndroidRuntime(332):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
11-30 02:52:03.035: E/AndroidRuntime(332):  ... 12 more
11-30 02:57:03.205: I/Process(332): Sending signal. PID: 332 SIG: 9

2번 째 그림의 이미지 불러오기를 누르면 그림과 같이 오류가 뜹니다.

카메라는 불러와 지는데 액티비티는 그냥 종료되어 버립니다.

 

여기저기 건드려 보긴 했는데 잘 안되서 여쭤 봅니다.

 

Notepadv3 은 전체 소스코드 입니다.

 

 처음 질문하시는 분은 공지사항을 다 읽었음 이라는 글을 질문 마지막에 적어주십시오