【发布时间】:2015-07-16 18:12:17
【问题描述】:
我读了很多堆栈溢出帖子,说在数据库中插入图像是不好的
Storing Images in DB - Yea or Nay 和 Storing image in database directly or as base64 data? 等等。
这件事对我来说仍然不明显在哪里保存图像?在服务器的文件中 ?.在我的应用程序中,用户将上传几张图片并在他的个人资料中查看它们。
一个例子将不胜感激。
我现在做了什么(我现在不是那么好,但我正在学习..)
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
//fnish inserting
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", name));
nameValuePairs.add(new BasicNameValuePair("password", pass));
nameValuePairs.add(new BasicNameValuePair("email", emails));
nameValuePairs.add(new BasicNameValuePair("image", image_str));
System.out.println(nameValuePairs);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://justedhak.comlu.com/users.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
【问题讨论】:
标签: android