从URL保存图像SQLite数据库 [英] Save image from URL to SQLITE Database
问题描述
我得到的一个问题,从网址插入图像SQLite数据库。
I'm getting a problem to insert an image from url to sqlite database.
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);
}
}
我创建如下数据库 -
I am creating a database as below-
final String[] creatStatments = new String[]{"create table "
+ IMG_table
+ "(account_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"+" account_image BLOB NOT NULL)",
以上所有code要保存URL图像SQLite数据库。但我得到一个异常,如下
All the above code to save image from url in sqlite database. But i am getting a Exception as below
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)
在我的经历,找到在哪里的问题,然后我发现,问题出在
When i go through to find where is problem then i found that problem is in
InputStream is = ucon.getInputStream();
所以,请有人帮我出这个问题。我真的会为我解决porblem在effort.Thanks事先都非常高兴。
So, please some one get me out of this problem. I will really very glad for my porblem resolving effort.Thanks in advance to all
推荐答案
这是不建议你的整个图像存储为您的SQLite数据库中的BLOB。相反,它下载到SD中的某个位置,并保存在你的SQLite数据库列该路径。此外,还有在android光标类,将不能正确处理的大小大于1MB大图像的错误。见<一href=\"http://stackoverflow.com/questions/6938405/how-to-load-more-than-1-mb-data-from-sqlite-db-to-android-cursor\">here.要下载图像的的AsyncTask
你可以使用这样的:
It is not recommended to store your entire image as a blob in your sqlite database. Instead download it to a certain location in sd and save that path in a column in your sqlite db. Also, there is a bug in the android Cursor class that will not handle correctly the images that are bigger in size than 1MB. See here. To download image from an AsyncTask
you can use something like this:
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();
}
}
}
这篇关于从URL保存图像SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!