Android 来自URL的Downlad PDF
我写了这段代码从URL下载文件。它下载了doc和jpg文件。但PDF文件未下载,显示com.android.volley.ClientError。URL包含此“શુભમ08_23_2016_06_05_33.pdf”。这个问题是因为url中的Gujrati字符吗 这是我的代码:Android 来自URL的Downlad PDF,android,file,pdf,download,android-volley,Android,File,Pdf,Download,Android Volley,我写了这段代码从URL下载文件。它下载了doc和jpg文件。但PDF文件未下载,显示com.android.volley.ClientError。URL包含此“શુભમ08_23_2016_06_05_33.pdf”。这个问题是因为url中的Gujrati字符吗 这是我的代码: InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mURL,
InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mURL,
new Response.Listener<byte[]>()
{
@Override
public void onResponse(byte[] response) {
// TODO handle the response
try {
if (response!=null) {
pd.dismiss();
FileOutputStream outputStream;
Features features=new Features(context);
String name=features.findName(mURL);;//<FILE_NAME_WITH_EXTENSION e.g reference.txt>
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/AIO");
if (!dir.isDirectory())
{
dir.mkdirs();
}
File file = new File(dir, name);
FileOutputStream f = new FileOutputStream(file);
//f=context.openFileOutput(name, Context.MODE_PRIVATE);
f.write(response);
f.close();
features.ToastCenter("File Downloaded in AIO Folder",true);
Intent intent = new Intent(Intent.ACTION_VIEW);
String extension=features.findExtension(mURL);
intent.setDataAndType(Uri.fromFile(file),"application/"+extension);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
boolean isFileOpened=false;
try
{
context.startActivity(intent);
isFileOpened=true;
}
catch (Exception e)
{
//Don't show any message here
}
if (!isFileOpened)
{
try
{
intent.setDataAndType(Uri.fromFile(file),"application/msword");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
catch (Exception e)
{
features.ToastCenter("Can not view file",true);
}
}
}
catch (ActivityNotFoundException e)
{
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
e.printStackTrace();
}
}
} ,new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Features features=new Features(context);
features.ToastCenter("Failed to Download File",true);
// TODO handle the error
error.printStackTrace();
pd.dismiss();
}
}, null);
RequestQueue mRequestQueue = Volley.newRequestQueue(context, new HurlStack());
mRequestQueue.add(request);
InputStreamVolleyRequest请求=新的InputStreamVolleyRequest(request.Method.GET,mURL,
新的Response.Listener()
{
@凌驾
公共void onResponse(字节[]响应){
//TODO处理响应
试一试{
if(响应!=null){
pd.解散();
FileOutputStream输出流;
特征=新特征(上下文);
字符串名称=features.findName(mURL)//
文件sdCard=Environment.getExternalStorageDirectory();
File dir=新文件(sdCard.getAbsolutePath()+“/AIO”);
如果(!dir.isDirectory())
{
dir.mkdirs();
}
文件=新文件(目录,名称);
FileOutputStream f=新的FileOutputStream(文件);
//f=context.openFileOutput(名称、context.MODE\u PRIVATE);
f、 写(应答);
f、 close();
ToastCenter(“在AIO文件夹中下载的文件”,true);
意向意向=新意向(意向.行动\视图);
字符串扩展=features.findExtension(mURL);
intent.setDataAndType(Uri.fromFile(文件),“application/”+扩展名);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
intent.setFlags(intent.FLAG\u活动\u新任务);
尝试
{
布尔值isFileOpened=false;
尝试
{
背景。开始触觉(意图);
isFileOpened=true;
}
捕获(例外e)
{
//此处不显示任何消息
}
如果(!isFileOpened)
{
尝试
{
intent.setDataAndType(Uri.fromFile(file),“application/msword”);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
intent.setFlags(intent.FLAG\u活动\u新任务);
背景。开始触觉(意图);
}
捕获(例外e)
{
ToastCenter(“无法查看文件”,true);
}
}
}
捕获(ActivityNotFounde异常)
{
}
}
}捕获(例外e){
//TODO自动生成的捕捉块
Log.d(“KEY_错误”,“无法下载文件”);
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
特征=新特征(上下文);
features.ToastCenter(“下载文件失败”,true);
//如何处理错误
错误。printStackTrace();
pd.解散();
}
},空);
RequestQueue MRRequestQueue=Volley.newRequestQueue(上下文,new HurlStack());
mRequestQueue.add(请求);
能否请您包含完整的错误日志,因为HTTP错误代码将特别有用。完整的日志仅显示:com.android.volley.clienterori下载了其他PDF文件,并且有效,这意味着它没有下载,因为字符在古吉拉特语中。所以请告诉我如何下载这种类型的文件代码>某处?