2

我在将图像从 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)",

以上所有代码都将图像从 url 保存到 sqlite 数据库中。但我得到一个例外如下

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();

所以,请有人帮我解决这个问题。我真的很高兴我解决问题的努力。在此先感谢大家

4

1 回答 1

3

不建议将整个图像作为 blob 存储在 sqlite 数据库中。而是将其下载到 sd 中的某个位置,并将该路径保存在您的 sqlite db 的列中。此外,android Cursor 类中存在一个错误,无法正确处理大于 1MB 的图像。见这里。要从图像下载图像,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();

    }

}

}
于 2013-07-05T07:14:17.543 回答