【问题标题】:How to download an image from server side to android using bytearray?如何使用 bytearray 将图像从服务器端下载到 android?
【发布时间】:2015-09-16 17:46:32
【问题描述】:
我的 Android 同事想要从Java Server 侧码发送图像,我尝试使用byte-array-output stream,这是将图像发送到Android client 的最佳方式。 android代码应该如何接收byte-array。
【问题讨论】:
标签:
android
image
bytearray
android-image
【解决方案1】:
您好,欢迎使用 stackoverflow。
有多种方法可以完成此任务,您要使用哪种方式取决于您的需求。
最常见的方式是使用 Socket(取决于需求)。
看到这个example。
此示例包括客户端和服务器端编码。
希望这会有所帮助。
【解决方案2】:
我已经通过字节数组完成了这些事情。这是我的代码供您参考:
String base64String = (String)sp.toString();
byte[] mediaData = Base64.decode(base64String, 0); write(notesList.get(position).getContent(), mediaData);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
notesList.get(position).getContent());
Uri path = Uri.fromFile(file);
if(notesList.get(position).getContent().contains(".pdf"))
{
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
context.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
}
}
else if(notesList.get(position).getContent().contains(".jpg") || notesList.get(position).getContent().contains(".jpeg") || notesList.get(position).getContent().contains(".png"))
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + path), "image/*");
context.startActivity(intent);
}
else if(notesList.get(position).getContent().contains(".xls"))
{
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/*");
List list = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
intent.setDataAndType(path, "application/*");
context.startActivity(intent);
}
}
这里,sp 是来自服务器的响应。