Android 将图像存储在数据库中是个坏主意,那么有什么替代方案呢?
我读过很多关于堆栈溢出的帖子,说在数据库中插入图像是不好的 还有很多其他的 这件事对我来说仍然不明显,在哪里保存图像?在服务器?中的文件中?。在我的应用程序中,用户将上传几个图像,并在其个人资料中看到它们 请举个例子 我现在所做的(我现在不太好,但我正在学习…)Android 将图像存储在数据库中是个坏主意,那么有什么替代方案呢?,android,Android,我读过很多关于堆栈溢出的帖子,说在数据库中插入图像是不好的 还有很多其他的 这件事对我来说仍然不明显,在哪里保存图像?在服务器?中的文件中?。在我的应用程序中,用户将上传几个图像,并在其个人资料中看到它们 请举个例子 我现在所做的(我现在不太好,但我正在学习…) Bitmap Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器); ByteArrayOutputStream=新建ByteArrayOutputS
Bitmap Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器);
ByteArrayOutputStream=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.PNG,90,stream)//压缩到您想要的格式。
byte[]byte_arr=stream.toByteArray();
字符串image\u str=Base64.encodeToString(byte\u arr,Base64.DEFAULT);
//fnish插入
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,名称));
添加(新的BasicNameValuePair(“密码”,pass));
添加(新的BasicNameValuePair(“电子邮件”),电子邮件);
添加(新的BasicNameValuePair(“image”,image_str));
System.out.println(nameValuePairs);
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://justedhak.comlu.com/users.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
System.out.println(响应);
HttpEntity=response.getEntity();
}捕获(客户端协议例外e){
}捕获(IOE异常){
如果您的图像重量(2KB-4KB),则可以以DB为单位保存
如果您的图像超过此权重,我建议使用SD卡。如果有许多用户在应用程序中发布图像。我应该将图像保存在哪里?如Fb或Tw?或者您知道和您的应用程序类似的示例?是的,如Fb,您所说的我的应用程序示例是什么意思
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) {