Android 메모 장 기능 구현

11830 단어 Android수첩
본 논문 의 사례 는 안 드 로 이 드 가 수첩 기능 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
기능 을 실현 하 다
1.텍스트 데이터 저장
2.이미지 데이터 저장
3.비디오 데이터 저장
4.사용자 정의 Adapter
5.SQLite 생 성
6.데이터 listview 목록 의 표시
demo 주소
수첩

인터페이스 레이아웃

<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:orientation="vertical" >

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >

 <Button
 android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="  " />

 <Button
 android:id="@+id/img"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="  " />

 <Button
 android:id="@+id/video"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="  " />
 </LinearLayout>

 <ListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" >
 </ListView>

</LinearLayout>

데이터베이스 생 성

public class NotesDB extends SQLiteOpenHelper {

 public static final String TABLE_NAME = "notes";
 public static final String CONTENT = "content";
 public static final String PATH = "path";
 public static final String VIDEO = "video";
 public static final String ID = "_id";
 public static final String TIME = "time";

 public NotesDB(Context context) {
 super(context, "notes", null, 1);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
 db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID
 + " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
 + " TEXT NOT NULL," + PATH + " TEXT NOT NULL," + VIDEO
 + " TEXT NOT NULL," + TIME + " TEXT NOT NULL)");
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

데이터 로드

public class AddContent extends Activity implements OnClickListener {

 private String val;
 private Button savebtn, deletebtn;
 private EditText ettext;
 private ImageView c_img;
 private VideoView v_video;
 private NotesDB notesDB;
 private SQLiteDatabase dbWriter;
 private File phoneFile, videoFile;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.addcontent);
 //     Button       
 val = getIntent().getStringExtra("flag");

 savebtn = (Button) findViewById(R.id.save);
 deletebtn = (Button) findViewById(R.id.delete);
 ettext = (EditText) findViewById(R.id.ettext);
 c_img = (ImageView) findViewById(R.id.c_img);
 v_video = (VideoView) findViewById(R.id.c_video);

 savebtn.setOnClickListener(this);
 deletebtn.setOnClickListener(this);

 notesDB = new NotesDB(this);
 dbWriter = notesDB.getWritableDatabase();

 initView();
 }
 //        ,  ,    ,        
 public void initView() {
 if (val.equals("1")) { //   
 c_img.setVisibility(View.GONE);
 v_video.setVisibility(View.GONE);
 }
 if (val.equals("2")) {
 c_img.setVisibility(View.VISIBLE);
 v_video.setVisibility(View.GONE);

 Intent img = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 phoneFile = new File(Environment.getExternalStorageDirectory()
  .getAbsoluteFile() + "/" + getTime() + ".jpg");
 img.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(phoneFile));
 startActivityForResult(img, 1);
 }
 if (val.equals("3")) {
 c_img.setVisibility(View.GONE);
 v_video.setVisibility(View.VISIBLE);

 Intent video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
 videoFile = new File(Environment.getExternalStorageDirectory()
  .getAbsoluteFile() + "/" + getTime() + ".mp4");
 video.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
 startActivityForResult(video, 2);
 }
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.save:
 addDB();
 finish();
 break;

 case R.id.delete:
 finish();
 break;
 }
 }
 //    
 public void addDB() {
 ContentValues cv = new ContentValues();
 cv.put(NotesDB.CONTENT, ettext.getText().toString());
 cv.put(NotesDB.TIME, getTime());
 cv.put(NotesDB.PATH, phoneFile + "");
 cv.put(NotesDB.VIDEO, videoFile + "");
 dbWriter.insert(NotesDB.TABLE_NAME, null, cv);
 }
 //     
 private String getTime() {
 SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd  HH:mm:ss");
 Date curDate = new Date();
 String str = format.format(curDate);
 return str;
 }
 //             
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);

 if (requestCode == 1) {
 Bitmap bitmap = BitmapFactory.decodeFile(phoneFile
  .getAbsolutePath());
 c_img.setImageBitmap(bitmap);
 }
 if (requestCode == 2) {
 v_video.setVideoURI(Uri.fromFile(videoFile));
 v_video.start();
 }
 }
}

사용자 정의 어댑터

public class MyAdapter extends BaseAdapter {

 private Context context;
 private Cursor cursor;
 private LinearLayout layout;

 public MyAdapter(Context context, Cursor cursor) {
 this.context = context;
 this.cursor = cursor;
 }

 @Override
 public int getCount() {
 return cursor.getCount();
 }

 @Override
 public Object getItem(int position) {
 return cursor.getPosition();
 }

 @Override
 public long getItemId(int position) {
 // TODO Auto-generated method stub
 return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
 LayoutInflater inflater = LayoutInflater.from(context);
 layout = (LinearLayout) inflater.inflate(R.layout.cell, null);

 TextView contenttv = (TextView) layout.findViewById(R.id.list_content);
 TextView timetv = (TextView) layout.findViewById(R.id.list_time);
 ImageView imgiv = (ImageView) layout.findViewById(R.id.list_img);
 ImageView videoiv = (ImageView) layout.findViewById(R.id.list_video);

 cursor.moveToPosition(position);

 String content = cursor.getString(cursor.getColumnIndex("content"));
 String time = cursor.getString(cursor.getColumnIndex("time"));
 String url = cursor.getString(cursor.getColumnIndex("path"));
 String urlvideo = cursor.getString(cursor.getColumnIndex("video"));

 contenttv.setText(content);
 timetv.setText(time);
 imgiv.setImageBitmap(getImageThumbnail(url, 200, 200));
 videoiv.setImageBitmap(getVideoThumbnail(urlvideo, 200, 200,
 MediaStore.Images.Thumbnails.MICRO_KIND));
 return layout;
 }
 /** ListView     */
 public Bitmap getImageThumbnail(String uri, int width, int height) {
 Bitmap bitmap = null;
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 bitmap = BitmapFactory.decodeFile(uri, options);
 options.inJustDecodeBounds = false;
 int beWidth = options.outWidth / width;
 int beHeight = options.outHeight / height;
 int be = 1;
 if (beWidth < beHeight) {
 be = beWidth;
 } else {
 be = beHeight;
 }
 if (be <= 0) {
 be = 1;
 }
 options.inSampleSize = be;
 bitmap = BitmapFactory.decodeFile(uri, options);
 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
 return bitmap;
 }
 /**     */
 private Bitmap getVideoThumbnail(String uri, int width, int height, int kind) {
 Bitmap bitmap = null;
 bitmap = ThumbnailUtils.createVideoThumbnail(uri, kind);
 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

 return bitmap;
 }

}

ListView 클릭 한 자세 한 페이지 표시 및 데이터 삭제

public class SelectAct extends Activity implements OnClickListener {

 private Button s_delete, s_back;
 private ImageView s_img;
 private VideoView s_video;
 private TextView s_tv;
 private NotesDB notesDB;
 private SQLiteDatabase dbWriter;
 /**   item               */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.select);

 s_delete = (Button) findViewById(R.id.s_delete);
 s_back = (Button) findViewById(R.id.s_back);
 s_img = (ImageView) findViewById(R.id.s_img);
 s_video = (VideoView) findViewById(R.id.s_video);
 s_tv = (TextView) findViewById(R.id.s_tv);
 /**    */
 notesDB = new NotesDB(this);
 dbWriter = notesDB.getWritableDatabase();

 s_back.setOnClickListener(this);
 s_delete.setOnClickListener(this);
 //        
 if (getIntent().getStringExtra(NotesDB.PATH).equals("null")) {
 s_img.setVisibility(View.GONE);
 } else {
 s_img.setVisibility(View.VISIBLE);
 }

 if (getIntent().getStringExtra(NotesDB.VIDEO).equals("null")) {
 s_video.setVisibility(View.GONE);
 } else {
 s_video.setVisibility(View.VISIBLE);
 }
 //          
 s_tv.setText(getIntent().getStringExtra(NotesDB.CONTENT));

 Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra(
 NotesDB.PATH));
 s_img.setImageBitmap(bitmap);

 s_video.setVideoURI(Uri
 .parse(getIntent().getStringExtra(NotesDB.VIDEO)));

 s_video.start();
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.s_delete:
 deleteDate();
 finish();
 break;

 case R.id.s_back:
 finish();
 break;
 }
 }
 //     
 public void deleteDate() {
 dbWriter.delete(NotesDB.TABLE_NAME,
 "_id=" + getIntent().getIntExtra(NotesDB.ID, 0), null);
 }
}

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기