- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用
http连接方式:获取
请求/响应:JSON
我有一些带有 ID 的记录(引用下图)。当用户单击记录时。我正在调用 HttpConnection(JSON 请求)到具有点击记录 ID 的服务器。
Json 请求 URL: http://xyz.in/api/mobile/document/12345/{record Id}
我从服务器获取 PDF(或)JPG(或)PNG 格式的 Json 响应的位置。
Json 响应:
%PDF-1.5
%����
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 51 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 11/Kids[ 3 0 R 10 0 R 18 0 R 21 0 R 24 0 R 27 0 R 30 0 R 33 0 R 36 0 R 39 0 R 47 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</ExtGState<</GS5 5 0 R/GS6 6 0 R>>/Font<</F1 7 0 R>>/XObject<</Image9 9 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 960 540] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 340>>
stream
x���Mk1����9�
fg&������ҥ=��U)T����MV[?.�fC��ɛY(��j���nC�ׅ)P!"1���!X���J�������S
���at����5�����.���$Tl)��˸6�����J��u:j�{µyGa�4�iuW�Gj0I?�U��u
�S��k4Z��N�7�T�T�Y��)�QY�b&�@��l��Ͼsr�{��R��?Cu+E�����g���9|�(͊Ϣ��r��)�e��5���R�N䬳q��oϥ�m6ټ����<��<47�=��sH�?��e�v��+����K�.���|ZBo��߶�
endstream
endobj
如果我在 Android Webview 中加载该 URL(http://xyz.in/api/mobile/document/12345/ {record Id}),则响应为 PDF,我可以查看它。但响应是 JPG 或 PNG。无法在 Webview 中查看。
如何在 Android 中处理它以供用户查看(或)下载。
提前致谢。
最佳答案
//Calling the Download file (Asynctask class).
new DownloadFileFromURL().execute(DownloadFileUrl);
//Progress dialog for download url
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
/**
* Background Async Task to download file
*/
class DownloadFileFromURL extends AsyncTask<String, String, String> {
String filename="";
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
String depo = conection.getHeaderField("Content-Disposition");
String depoSplit[] = depo.split("filename=");
filename = depoSplit[1].replace("filename=", "").replace("\"", "").trim();
Log.v("","fileName"+filename);
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/"+filename);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Reading filepath from sdcard
String FilePath = Environment.getExternalStorageDirectory().toString() + "/"+filename;
Log.v("FilePath", "" + FilePath);
}
}
关于Android-我从服务器获取 PDF(或)JPG(或)PNG 文件形式的 Json 响应。如何处理它以供用户查看(或)下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589800/
我的目录中有一堆图像(100+),它们的名称都不同。有什么方法可以将它们重命名为 0.jpg、1.jpg、2.jpg 等,可能使用脚本(我运行的是 Windows),而不必单独重命名每个文件?如果在
文件的顺序由可以嵌入文件名中的数字确定,但有时嵌入在名称的开头,例如文件1.txt文件2.txt文件3.txt文件10.txt文件11.txtETC..或者1.txt2.txt10.txt等. 重命名
我有一个 inotify 等待脚本,只要检测到文件已上传到源目录,它就会将文件从一个位置移动到另一个位置。 我面临的挑战是我需要保留文件的基本名称并将以下扩展名:.JPEG、.JPG、.jpeg 转换
我有一段代码可以上传图像并创建缩略图,只要扩展名是小写的 jpg,但如果是大写的 JPG,它就不会上传图像。当我重命名图像时,例如example.JPG 到 example.jpg,它会上传。但是 e
我必须缩小和解压缩一组大小为 4608 x 3456 的 JPG 图像。目前,我已经能够将图像正确解压缩为 RGB 格式并将它们转换为位图。 现在我需要实现缩小,根据我目前所读到的正确缩小图像的内容,
这个问题困扰我好久了。我正在尝试在 PHP 中加载图像,但是有 jpg 和 JPG 图像,当我尝试通过 jpg 加载图像时,找不到 JPG 图像(显然) $img1 = ''; $img2 = '';
我使用这个函数来检测我的文件是否存在。虽然我有一些图像存储为 .jpg、.JPG、.png 和 .PNG。但即使真实文件的扩展名为 .JPG 或 .PNG,它也总是将 .jpg 或 .png 返回为真
我有很多这样的文件: 13040-3BLK1.JPG 13040-3NAV11.JPG 13040-50NAV11.JPG 13040-60NAV11.JPG 13040-LNAV1.JPG
我在机器人上安装了摄像头。长话短说,相机输出二进制数组,我相信这是一个 JPG 编码流。我这么说是因为我使用的库中的许多方法都暗示它是 JPG 编码的,并且第一个和最后 2 个字节是 255 216
我正在制作个人网页,我已将其草稿上传到 www.kurtpeek.com。我注意到的一个错误是,我在“关于我”部分中的一张 jpeg 图片“MIT_IAP_SAR_smallest.JPG”没有出现在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
我有一张图片列表,我想将它们组合成每组一张图片。每个图像都有一个名称以及它是什么类型的图表。我希望有一种方法可以使用名称对图像进行分组,并为每个组返回单个 jpg 或 png 的输出。组合后的图像如下
我通过 JavaScript 设置 img 的 src 属性,如下所示: var newimgid = "images/" + fldrid + "/" + id + ".jpg"; document
我有一个 TImage,它显示文件夹中的 JPG 图像。每个 JPG 图像名称都是数字(例如 5968.jpg)。如果 JPG 图像不存在,我当前加载模板 JPG。 但是,我想在检查本地文件夹后再检查
我的脚本很简单。 SELECT file_name FROM images WHERE `id` = '$id' AND `file_name` LIKE '%_1.jpg' 当我在 mysql 中运
我有一个页面显示了一些产品,例如:- 标题- 描述- 图片 当图像存储在表中时,其存储为例如“image.jpg”...但是我想在上传图像时制作图像名称的标题将用作图像的标题产品的 html 标题(t
我想创建一个按钮来在 a.jpg 和 b.jpg 之间反复翻转图像。这是我的代码: function changeImg() { document.getElementById("
有没有人为 Windows 制作一个简单的 DLL,这样我就可以将原始图像数据保存为 JPG 格式?它也应该有 .lib 和 .h 文件,所以我不需要搞乱源代码编译,因为我发现它非常困难。 需要考虑的
我有一张我正在尝试创建的动态图像,它以前在一个网络主机上工作,直到我发现他们删除了一些功能,其中一些破坏了我的形象。我最近搬到了 hostgator,我也无法让图像在他们的网站上工作。实际的 PHP
有时我将 JPG 图像保存为未压缩的位图 (BMP/PNG),以在更改图像时保持质量。 我想知道,理论上是否可以将位图重新编码回其原始 JPG 格式,而不会损失任何质量(我编辑的区域除外)? 编辑:我
我是一名优秀的程序员,十分优秀!