我正在获取图像路径URL,现在我想从该URL获取图像并保存到本地数据库但是我无法将其转换为字节,以便数据库中的BLOB将接受它。我已经尝试了很多,但都是徒劳的。我使用了代码,但是ByteArrayBuffer没有解析类型。
// code to convert image url into byte array
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;
}
之后我使用了教程" https://github.com/CoderzHeaven/StoreImageSqliteAndroid"保存到数据库
答案 0 :(得分:0)
用这个替换你的方法
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");
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream accImage = new ByteArrayOutputStream();
while ((bytesRead = is.read(buffer)) != -1) {
accImage.write(buffer, 0, bytesRead);
}
return accImage.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
答案 1 :(得分:0)
**You can try this method for download and save image into 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();
new ImageDownloader().execute("http://images.100bestbuy.com/images/small_137385013870957.jpg");
}
/* get logo image method */
private byte[] getLogoImage(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
System.out.println("test1");
InputStream is = ucon.getInputStream();
System.out.println("test2");
BufferedInputStream bis = new BufferedInputStream(is);
System.out.println("test3");
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
System.out.println("test4");
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);
}
/* AsyncTask method */
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();
}
}
}