android将流图像直接输入SQL网络
我知道将图片导入BMP是:android将流图像直接输入SQL网络,android,sqlite,inputstream,Android,Sqlite,Inputstream,我知道将图片导入BMP是: String t1="http://www.xxx.xxx/xx/xx.jpg"; URL TKs = new URL(t1); InputStream Is= TKs.openStream(); Bitmap Bp = BitmapFactory.decodeStream(Is); Is.close(); 我还知道,将图片转换成可以存储在SQL中的格式: is = resources.openRawResource(R.drawable.xxx);
String t1="http://www.xxx.xxx/xx/xx.jpg";
URL TKs = new URL(t1);
InputStream Is= TKs.openStream();
Bitmap Bp = BitmapFactory.decodeStream(Is);
Is.close();
我还知道,将图片转换成可以存储在SQL中的格式:
is = resources.openRawResource(R.drawable.xxx);
byte[] image2 = new byte[is.available()];
is.read(image2);
is.close();
我使用这两种方法应该能够达到我的目标:
String t1="http://www.xxx.xxx/xx/xx.jpg";
URL TKs = new URL(t1);
InputStream Is= TKs.openStream();
byte[] image2 = new byte[Is.available()];
//////////////////the image2 is NULL
Is.read(image2);
Is.close();
显然我失败了
我想问我到底怎么做
public byte[] getImage(String t1) throws Exception{
URL url = new URL(t1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
return readStream(inStream);
}
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
byte[] data = getImage(t1);