我读了很多堆栈溢出帖子,说在数据库中插入图片不好
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) {
答案 0 :(得分:1)
如果你的图像重量(2KB-4KB),你可以保存在DB中
如果您的图像超过此重量,我建议使用SD卡。