我希望将图像发送到JSON webservice中的服务器(通过使用JSON post方法中的字符串参数),并希望从URI路径获取图像。
发布于 2011-08-04 09:30:14
下面的类是我用来从给定的url下载图片(在我的例子中是一个jpeg)的类。这是我自己代码的一部分,所以可能有一些特定于项目的东西在里面。只需阅读后面的内容:)。
public class BitmapFromUrl
{
private Bitmap myBitmap;
public BitmapFromUrl(String imageUrl)
{
URL myImageURL = null;
try
{
myImageURL = new URL(imageUrl);
}
catch (MalformedURLException error)
{
Log.e("tag", "The URL could not be formed from the provided String" + error);
}
if((myImageURL != null) && (imageUrl != null)) {
try
{
HttpURLConnection connection = (HttpURLConnection)myImageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
}
catch (IOException e)
{
Log.e("tag", "The Bitmap could not be downloaded or decoded!" + e);
}
} else {
Log.e("tag", "The provided URL(\"" + imageUrl + "\") does not seem to be valid.");
myBitmap = null;
}
}
public Bitmap getBitmap()
{
return myBitmap;
}
}要发送获取此图像的字符串,您可以使用以下命令:
Bitmap bm = BitmapFactory.decodeFile("/thePathToYour/image.jpeg");
ByteArrayOutputStream output = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, output); //bm is the bitmap object
byte[] bytes = output.toByteArray();
String base64Image = Base64.encode(bytes, Base64.DEFAULT);现在,您拥有了字符串形式的图像。在服务器上,您可以使用服务器编程语言的Base64方法将其改回图像。
json.put(WebConstant.JSON_PUT_PROPERTY_BUZZ_IMAGE, base64Image);这应该可以完成这项工作。
发布于 2011-08-05 05:37:07
在你的队伍中:
json.put(WebConstant.JSON_PUT_PROPERTY_BUZZ_IMAGE, " SEND IMAGE as URI ");//buzzInfoBean.getBuzzImage());"SEND IMAGE as URI“最有可能是base64编码的字符串,我不知道服务器期望的是什么,但这是最常见的。
查看this question的答案
从本地uri获取图像
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri1)https://stackoverflow.com/questions/6935104
复制相似问题