我在将图像从url插入sqlite数据库时出现问题。
public class MainActivity extends Activity {
protected SQLiteDatabase sqlitedatabase_obj;
DataBaseHelper databasehlpr_obj;
int accId;
byte[] accImage;
byte[] logoImage;
byte[] photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidContext.setContext(this);
sqlitedatabase_obj = DataBaseHelper.getInstance().getDb();
sqlitedatabase_obj.delete(DataBaseHelper.IMG_table, null, null);
logoImage = getLogoImage("http://images.bestbuy.com/images/small_137385013870957.jpg");
insertUser();
}
private byte[] getLogoImage(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
System.out.println("11111");
InputStream is = ucon.getInputStream();
System.out.println("12121");
BufferedInputStream bis = new BufferedInputStream(is);
System.out.println("22222");
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
System.out.println("23333");
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
photo = baf.toByteArray();
System.out.println("photo length" + photo);
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return accImage;
}
public void insertUser() {
ContentValues userdetailValues = new ContentValues();
userdetailValues.put("account_image", photo);
sqlitedatabase_obj.insert(DataBaseHelper.IMG_table, null, userdetailValues);
}
}
我正在创建一个数据库 -
final String[] creatStatments = new String[]{"create table "
+ IMG_table
+ "(account_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"+" account_image BLOB NOT NULL)",
以上所有代码都是在sqlite数据库中保存来自url的图像。但我得到的例外情况如下
Error: java.net.UnknownHostException: images.100bestbuy.com
Error inserting account_image=null
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
at com.example.urltodatabase.MainActivity.insertUser(MainActivity.java:75)
at com.example.urltodatabase.MainActivity.onCreate(MainActivity.java:39)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
当我找到问题的地方时,我发现问题在
InputStream is = ucon.getInputStream();
所以,请有人让我摆脱这个问题。我真的很高兴能够解决我的问题。谢谢所有人
答案 0 :(得分:3)
建议不要将整个图像作为blob存储在sqlite数据库中。而是将其下载到sd中的某个位置,并将该路径保存在sqlite db中的列中。此外,android Cursor类中存在一个错误,它无法正确处理大小超过1MB的图像。见here。要从AsyncTask
下载图像,您可以使用以下内容:
public class MainActivity extends Activity {
protected SQLiteDatabase sqlitedatabase_obj;
DataBaseHelper databasehlpr_obj;
int accId;
byte[] accImage;
byte[] logoImage;
byte[] photo;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidContext.setContext(this);
sqlitedatabase_obj = DataBaseHelper.getInstance().getDb();
new ImageDownloader().execute("http://images.100bestbuy.com/images/small_137385013870957.jpg");
}
private byte[] getLogoImage(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
System.out.println("11111");
InputStream is = ucon.getInputStream();
System.out.println("12121");
BufferedInputStream bis = new BufferedInputStream(is);
System.out.println("22222");
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
System.out.println("23333");
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
photo = baf.toByteArray();
System.out.println("photo length" + photo);
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return accImage;
}
public void insertUser() {
ContentValues userdetailValues = new ContentValues();
userdetailValues.put("account_image", photo);
sqlitedatabase_obj.insert(DataBaseHelper.IMG_table, null, userdetailValues);
}
private class ImageDownloader extends AsyncTask<String, Void, Void> {
private ProgressDialog progressDialog;
@Override
protected Void doInBackground(String... param) {
sqlitedatabase_obj.delete(DataBaseHelper.IMG_table, null, null);
logoImage = getLogoImage(param[0]);
insertUser();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,
"Wait", "Downloading Image");
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}
}