AndroidManifest xml



Download 74.58 Kb.
Date28.06.2017
Size74.58 Kb.
#21885
Aplikasi Database & Capture Photo

AndroidManifest.xml


"1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

package="com.tkj" android:versionCode="1" android:versionName="1.0">



"10" />

"android.permission.CAMERA" />

"android.hardware.camera" />

"android.hardware.camera.autofocus" />
"@drawable/icon" android:label="@string/app_name">

".DataSiswaFotoActivity"

android:label="@string/app_name">





"android.intent.action.MAIN" />

"android.intent.category.LAUNCHER" />





"Tambah Data" android:name="TambahData">




Database OpenHelper Class


package com.tkj;
import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;
public class MyDBOpenHelper extends SQLiteOpenHelper {

static final String DB_NAME = "MyDb1";

static final Integer DB_VERSION = 2;
public MyDBOpenHelper(Context context) {

super(context, DB_NAME, null, DB_VERSION);

}
@Override



public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE Siswa (" +

"nim TEXT PRIMARY KEY," +

"nama TEXT," +

"photo BLOB)");

}
@Override



public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.w("Upgrade",

"Proses akan drop dan buat ulang tabel.");

db.execSQL("DROP TABLE IF EXISTS Siswa");

onCreate(db);

}

}


Layout

main.xml





"1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">



"wrap_content"

android:layout_width="match_parent" android:id="@+id/android:list">





main_list.xml





"1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" android:orientation="horizontal">



"wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content" android:id="@+id/photo">

"@+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="match_parent">

"@+id/baris1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_width="match_parent">

"@+id/baris2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView">




tambahdata.xml


<
?xml version="1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

"@+id/textView1" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Nim">



"match_parent"

android:layout_height="wrap_content" android:id="@+id/editNim">







"@+id/textView2" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Nama">



"match_parent"

android:layout_height="wrap_content" android:id="@+id/editNama">





"wrap_content"

android:layout_width="wrap_content" android:src="@drawable/icon"

android:id="@+id/imagePhoto">





Application

ImageandTextAdapter.class


package com.tkj;
import java.util.ArrayList;

import java.util.HashMap;
import android.content.Context;

import android.graphics.Bitmap;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;
public class ImageandTextAdapter extends ArrayAdapter {

private LayoutInflater mInflater;

private int mViewResourceId;

private ArrayList> list;

public ImageandTextAdapter(Context context, int textViewResourceId, ArrayList> list) {

super(context, textViewResourceId);

// TODO Auto-generated constructor stub

mInflater = (LayoutInflater)context.getSystemService(

Context.LAYOUT_INFLATER_SERVICE);



this.list = list;

mViewResourceId = textViewResourceId;

}

@Override



public int getCount() {

return list.size();

}
@Override



public long getItemId(int position) {

return position;

}
@Override



public View getView(int position, View convertView, ViewGroup parent) {

convertView = mInflater.inflate(mViewResourceId, null);

//resize image base on screen density

float density = convertView.getResources().getDisplayMetrics().density;

int hwdpi = (int) (density*36);

ImageView iv = (ImageView)convertView.findViewById(R.id.photo);

Bitmap scaled = Bitmap.createScaledBitmap((Bitmap) list.get(position).get("photo"), hwdpi, hwdpi, true);

iv.setImageBitmap((Bitmap) scaled);


TextView baris1 = (TextView)convertView.findViewById(R.id.baris1);

baris1.setText((CharSequence) list.get(position).get("nim"));

TextView baris2 = (TextView)convertView.findViewById(R.id.baris2);

baris2.setText((CharSequence) list.get(position).get("nama"));



return convertView;

}

}



DataSiswaFotoActivity.java


package com.tkj;
import java.util.ArrayList;

import java.util.HashMap;
import android.app.ListActivity;

import android.content.ContentValues;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;
public class DataSiswaFotoActivity extends ListActivity {

private SQLiteDatabase db;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

openDatabase();

refreshView();

}

public void buttonTambahOnClick(View v) {

Intent intent = new Intent();

intent.setClass(this, TambahData.class);

startActivityForResult(intent, 1);

}

public void refreshView() {

ArrayList> list = new ArrayList>();

Cursor cursor = db.query(false, "Siswa", new String[] { "nim", "nama",

"photo" }, null, null, null, null, null, null);



while (cursor.moveToNext()) {

HashMap item = new HashMap();



byte[] byteArray=cursor.getBlob(cursor.getColumnIndex("photo"));

Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

item.put("photo", bitmap);

item.put("nim", cursor.getString(0));

item.put("nama", cursor.getString(1));

list.add(item);

}
ImageandTextAdapter notes = new ImageandTextAdapter(this, R.layout.main_list, list);

setListAdapter(notes);

}

@Override



protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK && requestCode == 1) {

ContentValues initialValues = new ContentValues();

initialValues.put("nim", data.getStringExtra("nim"));

initialValues.put("nama", data.getStringExtra("nama"));



byte[] byteArray = data.getByteArrayExtra("photo");

initialValues.put("photo", byteArray);



long hasil = db.insert("Siswa", null, initialValues);

if (hasil < 0) {

tampilToast("Data Gagal Disimpan!");

} else {

refreshView();

}

}

}



public void openDatabase() {

MyDBOpenHelper myOpenHelper = new MyDBOpenHelper(



this.getApplicationContext());

db = myOpenHelper.getWritableDatabase();

}
public void closeDatabase() {

db.close();

}

private void tampilToast(String pesan) {

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(getApplicationContext(), pesan, duration);

toast.show();

}

}



TambahData.java


package com.tkj;
import java.io.ByteArrayOutputStream;
import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;
public class TambahData extends Activity {

private static final int CAMERA_PIC_REQUEST = 1337;

Bitmap photo;


@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub



super.onCreate(savedInstanceState);

setContentView(R.layout.tambahdata);

}
public void buttonCaptureOnClick(View v) {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

startActivityForResult(intent, CAMERA_PIC_REQUEST);

}
@Override



public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK

&& requestCode == CAMERA_PIC_REQUEST) {

photo = (Bitmap) data.getExtras().get("data");

ImageView image = (ImageView) findViewById(R.id.imagePhoto);

image.setImageBitmap(photo);

}

}


public void buttonSimpanOnClick(View v) {

TextView nim = (TextView) findViewById(R.id.editNim);

TextView nama = (TextView) findViewById(R.id.editNama);

Intent intent = new Intent();

intent.putExtra("nim", nim.getText().toString());

intent.putExtra("nama", nama.getText().toString());


ByteArrayOutputStream stream = new ByteArrayOutputStream();

photo.compress(Bitmap.CompressFormat.PNG, 100, stream);



byte[] byteArray = stream.toByteArray();

intent.putExtra("photo", byteArray);



this.setResult(RESULT_OK, intent);

finish();



}

}


Download 74.58 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page