文件未发现异常使用.getInputStream当() [英] File not Found Exception When Using .getInputStream()

查看:93
本文介绍了文件未发现异常使用.getInputStream当()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用以下code下载一个远程托管.apk文件。

 包com.wireless.wmaa;进口的java.io.File;
进口java.io.FileOutputStream中;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口的java.net.URL;进口android.content.Context;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.util.Log;公共类UpdateApp扩展的AsyncTask<弦乐,太虚,太虚> {
私人上下文的背景下;
公共无效setContext(上下文contextf){
    上下文= contextf;
}@覆盖
保护无效doInBackground(字符串...为arg0){
      尝试{
            网址URL =新的URL(为arg0 [0]);
            HttpURLConnection的C =(HttpURLConnection类)url.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真);
            c.connect();            字符串PATH =/ SD卡/下载/;
            档案文件=新的文件(路径);
            file.mkdirs();
            文件OUTPUTFILE =新的文件(文件,Wireless.apk);
            如果(outputFile.exists()){
                outputFile.delete();
            }
            FOS的FileOutputStream =新的FileOutputStream(OUTPUTFILE);            InputStream为= c.getInputStream();
            字节[]缓冲区=新的字节[1024];
            INT LEN1 = 0;
            而((LEN1 = is.​​read(缓冲液))!= - 1){
                fos.write(缓冲液,0,LEN1);
            }
            fos.close();
            is.close();            意向意图=新意图(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(新文件(/ SD卡/下载/ Wireless.apk)),应用程序/ vnd.android.package归档);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //没有这个标志的android返回一个错误的意图!
            context.startActivity(意向);
        }赶上(例外五){
            Log.e(UpdateAPP+ e.getMessage()Update错误!);
        }
    返回null;
}}

和这里是code键启动任务:

  UpdateApp对myApp =新UpdateApp();
myApp.setContext(getApplicationContext());
myApp.execute(http://some.domain.com/someSite/some.apk);

能够连接,但尝试使用的InputStream时抛出未发现异常的文件。我可以浏览到从Android默认浏览器的文件,并提示我下载的是相同的功能,我想在我的应用程序文件。这里是我的清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.wireless.wmaa
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =10/>
    <使用特征的android:NAME =android.hardware.camera/>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =android.permission.CAMERA/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.DELETE_PACKAGES/>
    <使用许可权的android:NAME =android.permission.INSTALL_PACKAGES/>
    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
                                         >
        <活动
            机器人:标签=@字符串/ APP_NAME
            机器人:SelectServerNAME =
            机器人:主题=@安卓风格/ Theme.NoTitleBar
            机器人:screenOrientation =风景>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤波器>中
            < /活性GT;
        <活动
            机器人:名字=。UpdateApp
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.NoTitleBar
            机器人:screenOrientation =风景>
            &所述;意图滤光器>
            &所述; /意图滤光器>
        < /活性GT;        <活动
            机器人:名字=。WirelessActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.NoTitleBar
            机器人:screenOrientation =风景>
            &所述;意图滤光器>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
< /清单>


解决方案

结束了与IIS的问题。有一次,我动了我的apk文件到我的Apache网络服务器没有问题...

Trying to use the following Code to download a remote hosted .apk file.

package com.wireless.wmaa;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class UpdateApp extends AsyncTask<String,Void,Void>{
private Context context;
public void setContext(Context contextf){
    context = contextf;
}

@Override
protected Void doInBackground(String... arg0) {
      try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/sdcard/Download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "Wireless.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();


            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new                File("/sdcard/Download/Wireless.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag     android returned a intent error!
            context.startActivity(intent);


        } catch (Exception e) {
            Log.e("UpdateAPP", "Update error! " + e.getMessage());
        }
    return null;
}}  

And Here is the Code to start the Task:

UpdateApp myApp = new UpdateApp();
myApp.setContext(getApplicationContext());
myApp.execute("http://some.domain.com/someSite/some.apk");

Able to connect but when trying to use the InputStream throws a file not found exception. I can browse to the file from the Android Default Browser and I am prompted to download the file which is the same functionality I would like in my App. Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wireless.wmaa"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
                                         >
        <activity
            android:label="@string/app_name"
            android:name=".SelectServer" 
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter>"
            </activity>
        <activity
            android:name=".UpdateApp"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >  
            <intent-filter>
            </intent-filter>
        </activity>

        <activity
            android:name=".WirelessActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >  
            <intent-filter>
            </intent-filter>
        </activity>
    </application>


</manifest>

解决方案

Ended up being an Issue with IIS. Once I moved my .apk to my apache webserver had no issues...

这篇关于文件未发现异常使用.getInputStream当()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆