我读了很多堆栈溢出的帖子说在数据库中插入图像是不好的
Storing Images in DB - Yea or Nay和 Storing image in database directly or as base64 data?和许多其他人。
这件事对我来说还不是很明显在哪里保存图像?在服务器中的一个文件中 ?.在我的应用程序中,用户将上传几张图片并在他的个人资料中看到它们。
一个例子将不胜感激。
What I did for now (我现在做的不是很好但是我正在学习..)
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) {
最佳答案
如果你的图片有重量(2KB-4KB)你可以保存在数据库中
如果您的图像超过此重量,我建议使用 SD 卡。
关于android - 将图像存储在数据库中是个坏主意,那么有什么选择呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461588/