使用.getInputStream()时找不到文件异常

时间:2012-09-19 14:22:55

标签: android

尝试使用以下代码下载远程托管的.apk文件。

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;
}}  

以下是启动任务的代码:

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

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

<?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>

3 个答案:

答案 0 :(得分:1)

这一行File outputFile = new File(file, "Wireless.apk");

你错了

刚改变如下,

File outputFile = new File( PATH + "Wireless.apk");

也会更改以下代码

if(outputFile.exists())
{
     outputFile.delete();
}
else  // Add Else part, it is missing in your code
{
     outputFile.createNewFile();
}

答案 1 :(得分:0)

不要使用/sdcard/Downloads...这条路径在某些设备上有所不同。 你应该使用:

File file = Environment.getExternalStorageDirectory();

答案 2 :(得分:0)

结束了IIS的问题。一旦我将.apk移动到我的apache网络服务器就没有问题...