我尝试store image to sqlite from url
,如果我的连接很好但是如果连接fatal exception
,我会使用真实设备
12-27 13:28:05.248: E/AndroidRuntime(29456): FATAL EXCEPTION: main
12-27 13:28:05.248: E/AndroidRuntime(29456): java.lang.IllegalArgumentException: the bind value at index 2 is null
12-27 13:28:05.248: E/AndroidRuntime(29456): at android.database.sqlite.SQLiteProgram.bindBlob(SQLiteProgram.java:259)
12-27 13:28:05.248: E/AndroidRuntime(29456): at com.dbp.blob.MainActivity.Insert2DB(MainActivity.java:111)
12-27 13:28:05.248: E/AndroidRuntime(29456): at com.dbp.blob.MainActivity$ak.onPostExecute(MainActivity.java:63)
12-27 13:28:05.248: E/AndroidRuntime(29456): at com.dbp.blob.MainActivity$ak.onPostExecute(MainActivity.java:1)
12-27 13:28:05.248: E/AndroidRuntime(29456): at android.os.AsyncTask.finish(AsyncTask.java:417)
12-27 13:28:05.248: E/AndroidRuntime(29456): at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-27 13:28:05.248: E/AndroidRuntime(29456): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-27 13:28:05.248: E/AndroidRuntime(29456): at android.os.Handler.dispatchMessage(Handler.java:99)
这是代码
package com.dbp.blob;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
public class MainActivity extends Activity {
//url
private String IMAGEURL = "http://api.androidhive.info/json/movies/2.jpg";
ImageView ivImg;
//methode
private byte[] logoImage, ImageBlob;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
new ak().execute();
}
class ak extends AsyncTask<String, String, String>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
ivImg = (ImageView) findViewById(R.id.imageView1);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
logoImage = getLogoImage(IMAGEURL);
// insert into DB
return null;
}
protected void onPostExecute(String convert){
pDialog.dismiss();
Insert2DB();
// retrieve from DB
RetrieveImageFromDB();
// Display Image
ivImg.setImageBitmap(getImage(ImageBlob));
}
}
//ambil logo
private byte[] getLogoImage(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
//proses insert
public void Insert2DB() {
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "INSERT INTO PICTURES (pic_name,picture) VALUES(?,?)";
SQLiteStatement insertStmt = db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, "Image");
insertStmt.bindBlob(2, logoImage);
insertStmt.executeInsert();
db.close();
}
//proses display
private void RetrieveImageFromDB() {
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "SELECT * FROM PICTURES";
Cursor cursor = db.rawQuery(sql, new String[] {});
if (cursor.moveToFirst()) {
@SuppressWarnings("unused")
String strName;
strName = cursor.getString(1);
//inisialisasi image
ImageBlob = cursor.getBlob(2);
}
cursor.close();
db.close();
}
//decode
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
答案 0 :(得分:0)
Insert and retrieve ur image in doInBackground
protected String doInBackground(String... args) {
logoImage = getLogoImage(IMAGEURL);
// insert into DB
Insert2DB();
// retrieve from DB
RetrieveImageFromDB();
return null;
}
protected void onPostExecute(String convert){
pDialog.dismiss();
// Display Image
ivImg.setImageBitmap(getImage(ImageBlob));
}
}