제가 이번에 카메라크롭을 이용해 이미지를 보여주고 이미지에 관련된 글을 작성하고(edittext)
이미지,글 을 저장하는 db를 연동해서 저장 후, 검색?!하면 볼 수 있는 기능들을 구현 하고 싶습니다.
검색을 통해 좋은 글과 설명때문에 도움을 많이 받았습니다.(cameracrop 등등)
다음 설명들은 제가 첨부 파일에 올려 놓겠습니다.(첨부 파일이 안올라 갈 수 도있습니다. duals357@naver.com 제 메일입니다.)
많은 조언 과 도움 부탁드립니다. 감사합니다.( 제 코드가 이상하면 다르게 바꾸셔서 도움을 주셔도 됩니다.)
package pe.kr.theeye.cameracrop;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
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;
import java.io.File;
public class MainActivity extends Activity implements OnClickListener
{
SQLiteDatabase db;
boolean databaseCreated = false;
boolean tableCreated = false;
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;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button);
mPhotoImageView = (ImageView) findViewById(R.id.image);
mButton.setOnClickListener(this);
}
/**
* 카메라에서 이미지 가져오기
*/
private void doTakePhotoAction()
{
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;
}
}
}
@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(this)
.setTitle("업로드할 이미지 선택")
.setPositiveButton("사진촬영", cameraListener)
.setNeutralButton("앨범선택", albumListener)
.setNegativeButton("취소", cancelListener)
.show();
}}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- hotizontal가로로 정렬 -->
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="이미지 가져오기"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:layout_margin="20dp"
android:background="#eee" />
<EditText
android:id="@+id/input"
android:layout_width="370dp"
android:layout_height="250dp"
android:hint="입력부분"
android:inputType="textMultiLine"
android:privateImeOptions="defaultInputmode=korean;"
android:layout_alignParentStart="true"
android:scrollbars="vertical"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"> //부모 기준 오른 정렬
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="46dp"
android:text="저장하기" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="46dp"
android:text="뒤로가기" />
</LinearLayout>
</LinearLayout>
</LinearLayout>