这是我的代码主要活动
public class MainActivity extends Activity {
Button Btngetdata;
// URL to get JSON Array
private static String url = "http://10.0.2.2:81/bantulfolder/alldata.php";
private String IMAGEURL = "http://10.0.2.2:81/bantulfolder/";
private byte[] logoImage;
// JSON Node Names
private static final String TAG_DATA = "data";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_IMAGE = "image";
JSONArray user = null;
private DatabaseHandler databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHandler(MainActivity.this);
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
final String TAG = "AsyncTaskParseJson.java";
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
user = json.getJSONArray(TAG_DATA);
// loop through all users
for (int i = 0; i < user.length(); i++) {
JSONObject c = user.getJSONObject(i);
// Storing JSON item in a Variable
String idt = c.getString(TAG_IDT);
String idl = c.getString(TAG_IDL);
String nama = c.getString(TAG_NAMA);
String alamat = c.getString(TAG_ALAMAT);
String image = c.getString(TAG_IMAGE);
// code for get image url and make byte
logoImage = getLogoImage(IMAGEURL+image);
databaseHelper.saveCategoryRecord(idt, idl, nama, alamat, image, logoImage);
// Set JSON Data in TextView
// uid.setText(idt);
//name1.setText(name);
// email1.setText(email);
// show the values in our logcat
Log.e(TAG, "nama >>>> " + idt + " alamat>>>>> " + nama);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
// try get url and make byte
private byte[] getLogoImage(String geturl) {
try {
URL imageUrl = new URL(geturl);
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;
}
}
这是mydatabase处理程序
public class DatabaseHandler {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "bantul";
private static final String TABLE_NAME = "tb_alldata";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_IMAGE = "image";
public static final String TAG_FILE_IMAGE = "file_image";
Category openHelper;
private SQLiteDatabase database;
public DatabaseHandler(Context context){
openHelper = new Category(context);
database = openHelper.getWritableDatabase();
}
public void saveCategoryRecord(String idt, String idl, String nama, String alamat, String image, byte[] logoImage) {
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_IDT, idt);
contentValues.put(TAG_IDL, idl);
contentValues.put(TAG_NAMA, nama);
contentValues.put(TAG_ALAMAT, alamat);
contentValues.put(TAG_IMAGE, image);
contentValues.put(TAG_FILE_IMAGE, logoImage);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getTimeRecordList() {
return database.rawQuery("select * from " + TABLE_NAME, null);
}
private class Category extends SQLiteOpenHelper {
public Category(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ TAG_IDT + " INTEGER PRIMARY KEY, "
+ TAG_IDL + " INTEGER, "+ TAG_NAMA + " TEXT, "+ TAG_ALAMAT + " TEXT, "+ TAG_IMAGE + " TEXT, "
+ TAG_FILE_IMAGE + " BLOB, )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:1)
这不是你问题的答案:虽然可以做到这一点,但这可能是一个坏主意。
SQLite效率取决于“页面”的概念。您可以越多地保留对单个页面的引用,您的数据库执行得越好。
当您开始使用BLOB时,不仅单个行不会完全位于单个页面上,而且单个BLOB可能不在单个页面上。这将大大减慢访问时间。
您可以设置页面大小,但这需要大量内存。
如果必须将图像保存在数据库中,我建议您将它们保存在单独的数据库中。在主数据库中设置页面大小默认值,并在另一个数据库中设置适合您的BLOB大小。将BLOB的PK存储在主数据库中。
更好的是,只需使用随机名称将图像保存在文件系统上,并将名称存储在主DB中。 ContentProviders甚至支持openFile方法,使其变得非常简单。
您的代码实际上看起来是正确的。也许它就像TAG_FILE_GAMAR一样简单!= TAG_FILE_IMAGE?
答案 1 :(得分:0)
正如@G所建议的那样。 Blake Meike而不是BLOB,使用String来存储你的图像。
这里你可以编码字节:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);