I'm currently having problem to store and retrieve image from the database. All is set but the program stops while clicking on the button to save and get the picture from the database. The error shows in my Log cat as "could not execute method of activity".
Please help me out! Below is the code.
DATABASEHELPER.java
package com.example.image_database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.ImageView;
class DatabaseHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table if not exists tb(a blob)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity.java
package com.example.image_database;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView image1;
SQLiteDatabase db;
Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) this.findViewById(R.id.image1);
btn1 = (Button) this.findViewById(R.id.save);
btn2 = (Button) this.findViewById(R.id.get);
}
public void saveImage(View v)
{
try {
FileInputStream fis = new FileInputStream("/storage/sdcard/4.jpg");
try {
byte[] image = new byte[fis.available()];
fis.read(image);
ContentValues values = new ContentValues();
values.put("a", image);
db.insert("tb", null, values);
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getImage(View v)
{
Cursor c = db.rawQuery("Select * from tb", null);
if(c.moveToNext())
{
byte[] image = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,image.length);
image1.setImageBitmap(bmp);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.image_database.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/save"
android:layout_width="254dp"
android:layout_gravity="left"
android:text="save"
android:onClick="saveImage" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/get"
android:layout_width="match_parent"
android:layout_gravity="left"
android:text="get"
android:onClick="getImage" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="234dp"
android:layout_gravity="left"
android:src="@drawable/ic_launcher" />
</GridLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
Aucun commentaire:
Enregistrer un commentaire