Android:无法在内部存储器中写入

时间:2016-07-19 08:08:26

标签: android

我正在尝试从互联网上下载.apk文件并将其保存到内存然后安装它。我更喜欢应用程序私有内存,但我无法做到这一点,我必须使用外部存储内存。这里发生了什么

1. This works fine. path is /storage/emulated/0/Download/monotype
    File str = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File sdDir = new File(str , "monotype");

2. This works fine. Path is  /storage/emulated/0/AdroidData/com.monotype.myAppName/files

File str2 = contextInTask.getExternalFilesDir(null);
File sdDir = new File(str , "monotype");

3. This gives me "Parse Error: There was problem while parsing the package" when I try to install the apk saved. 
//Path is /data/user/0/com.monotype.myAppName/app_monotype.

ContextWrapper cw = new ContextWrapper(contextInTask);
File sdDir1 = cw.getDir("monotype", Context.MODE_PRIVATE);

4. This gives me "Parse Error: There was problem while parsing the package" when I try to install the apk saved.
//Path is /data/user/0/com.monotype.myAppName/files/monotype

File sdDir2 = contextInTask.getFilesDir();
File sdDir = new File(str , "monotype");

 Case 5: This gives me "java.io.IOException: open failed: ENOENT (No such file or directory)" 
//when I try to create the file using "File.createNewFile();". 
//Path is /storage/emulated/0/
File str = Environment.getExternalStorageDirectory();
File sdDir = new File(str , "monotype");

Case 6: This gives me "Parse Error: There was problem while parsing the package" when I try to install the apk saved. 
FileOutputStream os = contextInTask.openFileOutput("FileName",contextInTask.MODE_PRIVATE);

在案例3和4中:我的设备已植根。所以我能够访问保存的文件并且它没有损坏。它工作正常。所以我无法从这些文件夹安装apk。

案例5:我想我可以假设我不允许在外部文件夹中创建新文件夹。所以这个错误可以以某种方式解释,但我觉得这应该在创建目录时给我错误

以下是我存储和安装apk文件的异步任务代码

public class FontLoader extends AsyncTask<String, Void, String> {
    private final String LOG_TAG = FontLoader.class.getSimpleName();
    Context contextInTask;
    String fontName;
    Activity activityInTask;
    int ACTIVITY_INT = 5000;

    public FontLoader(Context context, String fn, Activity activity) {
        this.contextInTask = context;
        this.fontName = fn;
        this.activityInTask = activity;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection urlConnection = null;

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream == null) {
                // Nothing to do.
                return null;
            }

            File str = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            //File str = Environment.getExternalStorageDirectory();
            //File str2 = contextInTask.getExternalFilesDir(null);

            //ContextWrapper cw = new ContextWrapper(contextInTask);
            //File sdDir1 = cw.getDir("monotype", Context.MODE_PRIVATE);

            //File sdDir2 = contextInTask.getFilesDir();

            File sdDir = new File(str , "monotype");
            if (!sdDir.exists())
            {
                sdDir.mkdirs();
            }
            this.fontName = this.fontName + ".apk";
            File outputApk = new File(sdDir,this.fontName);

            if (outputApk.exists()) {
                outputApk.delete();
            }

            outputApk.createNewFile();

            Handler handler =  new Handler(contextInTask.getMainLooper());
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(contextInTask, "Application is being downloaded. Please wait",
                            Toast.LENGTH_SHORT).show();
                }
            });

            FileOutputStream os = new FileOutputStream(outputApk);

            byte[] tempArray = new byte[1024];
            int len = 0, total=0;

            while ((len = inputStream.read(tempArray)) != -1)
            {
                total += len;
                os.write(tempArray, 0, len);
            }

            os.flush();
            os.close();
            inputStream.close();
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(contextInTask, "Application has been downloaded",
                            Toast.LENGTH_SHORT).show();
                }
            });

            return outputApk.toString();

        } catch (IOException e) {
            Log.e(LOG_TAG, "Error ", e);
            // If the code didn't successfully get the weather data, there's no point in attemping
            // to parse it.
            return null;
        }
    }

@Override
protected void onPostExecute(String apkPath) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    File file = new File(apkPath);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    activityInTask.startActivityForResult(intent, ACTIVITY_INT);
}

}

0 个答案:

没有答案