Android记事本的开发

Android记事本的开发

可以进行文字,图片,视频的保存与删除

代码实现:

1、创建数据库NotesDB.java

package com.jikexueyuan.hellonotes;  import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;  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) {  	}  } 

2、建立适配器MyAdapter.java

package com.jikexueyuan.hellonotes;  import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;  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); 		videoiv.setImageBitmap(getVideoThumbnail(urlvideo, 200, 200, 				MediaStore.Images.Thumbnails.MICRO_KIND)); 		imgiv.setImageBitmap(getImageThumbnail(url, 200, 200)); 		return layout; 	}  	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; 	}  } 

MainActivity.java

package com.jikexueyuan.hellonotes;  import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView;  public class MainActivity extends Activity implements OnClickListener {  	private Button textbtn, imgbtn, videobtn; 	private ListView lv; 	private Intent i; 	private MyAdapter adapter; 	private NotesDB notesDB; 	private SQLiteDatabase dbReader; 	private Cursor cursor;  	@Override 	protected void onCreate(Bundle savedInstanceState) { 		super.onCreate(savedInstanceState); 		setContentView(R.layout.activity_main); 		initView(); 	}  	public void initView() { 		lv = (ListView) findViewById(R.id.list); 		textbtn = (Button) findViewById(R.id.text); 		imgbtn = (Button) findViewById(R.id.img); 		videobtn = (Button) findViewById(R.id.video); 		textbtn.setOnClickListener(this); 		imgbtn.setOnClickListener(this); 		videobtn.setOnClickListener(this); 		notesDB = new NotesDB(this); 		dbReader = notesDB.getReadableDatabase(); 		lv.setOnItemClickListener(new OnItemClickListener() {  			@Override 			public void onItemClick(AdapterView<?> parent, View view, 					int position, long id) { 				cursor.moveToPosition(position); 				Intent i = new Intent(MainActivity.this, SelectAct.class); 				i.putExtra(NotesDB.ID, 						cursor.getInt(cursor.getColumnIndex(NotesDB.ID))); 				i.putExtra(NotesDB.CONTENT, cursor.getString(cursor 						.getColumnIndex(NotesDB.CONTENT))); 				i.putExtra(NotesDB.TIME, 						cursor.getString(cursor.getColumnIndex(NotesDB.TIME))); 				i.putExtra(NotesDB.PATH, 						cursor.getString(cursor.getColumnIndex(NotesDB.PATH))); 				i.putExtra(NotesDB.VIDEO, 						cursor.getString(cursor.getColumnIndex(NotesDB.VIDEO))); 				startActivity(i); 			} 		}); 	}  	@Override 	public void onClick(View v) { 		i = new Intent(this, AddContent.class); 		switch (v.getId()) { 		case R.id.text: 			i.putExtra("flag", "1"); 			startActivity(i); 			break;  		case R.id.img: 			i.putExtra("flag", "2"); 			startActivity(i); 			break;  		case R.id.video: 			i.putExtra("flag", "3"); 			startActivity(i); 			break; 		} 	}  	public void selectDB() { 		cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, 				null, null); 		adapter = new MyAdapter(this, cursor); 		lv.setAdapter(adapter); 	}  	@Override 	protected void onResume() { 		super.onResume(); 		selectDB(); 	}  <span style="color:#ff0000;">} </span>

另外拍照和视频,都以位图的缩略图进行显示

这是图片的位图

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; 	}

UI布局

<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"     tools:context=".MainActivity" >      <LinearLayout         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:orientation="horizontal" >          <Button             android:id="@+id/btnText"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             android:text="文字" />          <Button             android:id="@+id/btnimg"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             android:text="图文" />          <Button             android:id="@+id/btnvideo"             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>


下面就不一一介绍了




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值