gpt4 book ai didi

Android-我从服务器获取 PDF(或)JPG(或)PNG 文件形式的 Json 响应。如何处理它以供用户查看(或)下载

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:29 24 4
gpt4 key购买 nike

我正在使用

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com