从本地文件安装apk

时间:2017-11-30 10:25:55

标签: java android

我创建了一个应用程序,我从内部服务器下载APK,将其保存在本地并希望提示用户安装它。 我的代码如下:

        Button bt_install = (Button) findViewById(R.id.install_bt);
        bt_install.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DownloadNewVersion().execute();
            }
        });
    }
    class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            bar = new ProgressDialog(MainActivity.this);
            bar.setCancelable(false);
            bar.setMessage("Downloading...");
            bar.setIndeterminate(true);
            bar.setCanceledOnTouchOutside(false);
            bar.show();
        }
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            bar.setIndeterminate(false);
            bar.setMax(100);
            bar.setProgress(progress[0]);
            String msg = "";
            if(progress[0]>99){
                msg="Finishing... ";
            }else {
                msg="Downloading... "+progress[0]+"%";
            }
            bar.setMessage(msg);
        }
        @Override
        protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
            super.onPostExecute(result);
            bar.dismiss();
            if(result){
                Toast.makeText(getApplicationContext(),"Update Done",
                        Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(),"Error: Try Again",
                        Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        protected Boolean doInBackground(String... arg0) {
            Boolean flag = false;
            try {
                URL url = new URL("http://androidpala.com/tutorial/app-debug.apk");
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
                String PATH = Environment.getExternalStorageDirectory()+"/Download/";
                File file = new File(PATH);
                file.mkdirs();
                File outputFile = new File(file,"app-debug.apk");
                if(outputFile.exists()){
                    outputFile.delete();
                }
                FileOutputStream fos = new FileOutputStream(outputFile);
                InputStream is = c.getInputStream();
                int total_size = 1431692;//size of apk
                byte[] buffer = new byte[1024];
                int len1 = 0;
                int per = 0;
                int downloaded=0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                    downloaded +=len1;
                    per = (int) (downloaded * 100 / total_size);
                    publishProgress(per);
                }
                fos.close();
                is.close();
                OpenNewVersion(PATH);
                flag = true;
            } catch (Exception e) {
                Log.e(TAG, "Update Error: " + e.getMessage());
                flag = false;
            }
            return flag;
        }
    }
    void OpenNewVersion(String location) {
        String filepath = Environment.getExternalStorageDirectory().getPath() +"/Download/"+"app-debug.apk";
        Uri fileLoc = Uri.fromFile(new File(filepath));
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(fileLoc, "application/vnd.android.package-archive");
        startActivity(intent);
    }




}

该路径包含APK的文件,因此下载没有问题,但在尝试启动活动时获取更新错误。

我在这里做错了什么?

logcot

11-30 14:30:54.075 5380-5380/net.amjadroid.fonts E/Zygote: v2
11-30 14:30:54.075 5380-5380/net.droid5d.myapp I/libpersona: KNOX_SDCARD checking this for 10342
11-30 14:30:54.075 5380-5380/net.droid5d.myapp I/libpersona: KNOX_SDCARD not a persona
11-30 14:30:54.075 5380-5380/net.droid5d.myapp E/Zygote: accessInfo : 0
11-30 14:30:54.076 5380-5380/net.droid5d.myapp W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.1.1_0001, [-1 -1 -1 -1 0 1]
11-30 14:30:54.076 5380-5380/net.droid5d.myapp I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=net.droid5d.myapp 
11-30 14:30:54.079 5380-5380/net.droid5d.myapp I/art: Late-enabling -Xcheck:jni
11-30 14:30:54.148 5380-5380/net.droid5d.myapp V/ActivityThread: performLaunchActivity: mActivityCurrentConfig={0 1.0 themeSeq = 0 showBtnBg = 0 422mcc2mnc [ar_AE,en_US] ldrtl sw411dp w411dp h773dp 560dpi nrml long port ?dc finger -keyb/v/h -nav/h mkbd/h desktop/d s.8}
11-30 14:30:54.153 5380-5380/net.droid5d.myapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-30 14:30:54.182 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:54.183 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:54.188 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:54.200 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: ThreadedRenderer.create() translucent=false
11-30 14:30:54.203 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=69
11-30 14:30:54.203 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: setView = DecorView@aa21a73[SplashScreen] touchMode=true
11-30 14:30:54.208 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: dispatchAttachedToWindow
11-30 14:30:54.225 5380-5380/net.droid5d.myapp V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
11-30 14:30:54.225 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2960] result=0x27 surface={isValid=true 529685839872} surfaceGenerationChanged=true
11-30 14:30:54.225 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: mHardwareRenderer.initialize() mSurface={isValid=true 529685839872} hwInitialized=true
11-30 14:30:54.259 5380-5408/net.droid5d.myapp D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
11-30 14:30:54.274 5380-5408/net.droid5d.myapp I/OpenGLRenderer: Initialized EGL, version 1.4
11-30 14:30:54.274 5380-5408/net.droid5d.myapp D/OpenGLRenderer: Swap behavior 1
11-30 14:30:54.278 5380-5408/net.droid5d.myapp D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2960]-format:1
11-30 14:30:54.303 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 168) vi=Rect(0, 84 - 0, 168) or=١
11-30 14:30:54.303 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: MSG_WINDOW_FOCUS_CHANGED 1
11-30 14:30:54.303 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 529685839872}
11-30 14:30:54.304 5380-5380/net.droid5d.myapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@49264c7 nm : net.droid5d.myapp ic=null
11-30 14:30:54.304 5380-5380/net.droid5d.myapp I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
11-30 14:30:54.310 5380-5400/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=79
11-30 14:30:54.313 5380-5380/net.droid5d.myapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@5f3baf4 nm : net.droid5d.myapp ic=null
11-30 14:30:55.666 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: ViewPostImeInputStage processPointer 0
11-30 14:30:55.775 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: ViewPostImeInputStage processPointer 1
11-30 14:30:55.800 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: MSG_WINDOW_FOCUS_CHANGED 0
11-30 14:30:55.814 5380-5380/net.droid5d.myapp V/ActivityThread: performLaunchActivity: mActivityCurrentConfig={0 1.0 themeSeq = 0 showBtnBg = 0 422mcc2mnc [ar_AE,en_US] ldrtl sw411dp w411dp h773dp 560dpi nrml long port ?dc finger -keyb/v/h -nav/h mkbd/h desktop/d s.8}
11-30 14:30:55.824 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.824 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.828 5380-5380/net.droid5d.myapp D/ScrollView: initGoToTop
11-30 14:30:55.830 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.832 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.833 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.835 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.835 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:55.846 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: ThreadedRenderer.create() translucent=false
11-30 14:30:55.848 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=84
11-30 14:30:55.848 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: setView = DecorView@f6ce7d8[MainActivity] touchMode=true
11-30 14:30:55.850 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: dispatchAttachedToWindow
11-30 14:30:55.874 5380-5380/net.droid5d.myapp V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
11-30 14:30:55.875 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2960] result=0x27 surface={isValid=true 529815250432} surfaceGenerationChanged=true
11-30 14:30:55.875 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 529815250432} hwInitialized=true
11-30 14:30:55.877 5380-5408/net.droid5d.myapp D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2960]-format:1
11-30 14:30:55.877 5380-5380/net.droid5d.myapp D/ScrollView:  onsize change changed 
11-30 14:30:55.906 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 168) vi=Rect(0, 84 - 0, 168) or=١
11-30 14:30:55.906 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
11-30 14:30:55.906 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 529815250432}
11-30 14:30:55.907 5380-5380/net.droid5d.myapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@2b0f233 nm : net.droid5d.myapp ic=null
11-30 14:30:55.907 5380-5380/net.droid5d.myapp I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
11-30 14:30:55.917 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=81
11-30 14:30:55.917 5380-5380/net.droid5d.myapp D/InputTransport: Input channel destroyed: fd=79
11-30 14:30:55.974 5380-5408/net.droid5d.myapp D/OpenGLRenderer: endAllActiveAnimators on 0x7b5b67dc00 (RippleDrawable) with handle 0x7b53a971e0
11-30 14:30:55.974 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: mHardwareRenderer.destroy()#1
11-30 14:30:55.978 5380-5380/net.droid5d.myapp D/ViewRootImpl@3cea6e2[SplashScreen]: Relayout returned: oldFrame=[0,0][1440,2960] newFrame=[0,0][1440,2960] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true
11-30 14:30:56.370 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: ViewPostImeInputStage processPointer 0
11-30 14:30:56.479 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: ViewPostImeInputStage processPointer 1
11-30 14:30:56.485 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:56.492 5380-5380/net.droid5d.myapp D/ScrollView: initGoToTop
11-30 14:30:56.494 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:56.496 5380-5380/net.droid5d.myapp D/ScrollView: initGoToTop
11-30 14:30:56.498 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:56.499 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:56.500 5380-5380/net.droid5d.myapp D/TextView: setTypeface with style : 0
11-30 14:30:56.503 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: ThreadedRenderer.create() translucent=true
11-30 14:30:56.506 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=76
11-30 14:30:56.506 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: setView = DecorView@e541050[MainActivity] touchMode=true
11-30 14:30:56.507 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: dispatchAttachedToWindow
11-30 14:30:56.509 5380-5442/net.droid5d.myapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-30 14:30:56.510 5380-5442/net.droid5d.myapp I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
11-30 14:30:56.510 5380-5442/net.droid5d.myapp I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
11-30 14:30:56.513 5380-5442/net.droid5d.myapp D/TcpOptimizer: TcpOptimizer-ON
11-30 14:30:56.524 5380-5380/net.droid5d.myapp V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
11-30 14:30:56.525 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[37,1235][1403,1641] result=0x27 surface={isValid=true 529685839872} surfaceGenerationChanged=true
11-30 14:30:56.525 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 529685839872} hwInitialized=true
11-30 14:30:56.526 5380-5408/net.droid5d.myapp D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1590x630]-format:1
11-30 14:30:56.526 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
11-30 14:30:56.526 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 529685839872}
11-30 14:30:56.536 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(37, 1235 - 1403, 1641) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=١
11-30 14:30:56.555 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
11-30 14:31:14.568 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: ViewPostImeInputStage processPointer 0
11-30 14:31:14.725 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: ViewPostImeInputStage processPointer 1
11-30 14:31:22.942 5380-5442/net.droid5d.myapp E/MainActivity: Update Error: file:///storage/emulated/0/Download/app-debug.apk exposed beyond app through Intent.getData()
11-30 14:31:22.984 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: mHardwareRenderer.destroy()#4
11-30 14:31:22.984 5380-5380/net.droid5d.myapp D/ViewRootImpl@86b8613[MainActivity]: dispatchDetachedFromWindow
11-30 14:31:22.997 5380-5380/net.droid5d.myapp D/InputTransport: Input channel destroyed: fd=76
11-30 14:31:22.998 5380-5380/net.droid5d.myapp W/SemDesktopModeManager: unregisterListener: Listener is null
11-30 14:31:23.019 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
11-30 14:31:23.020 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 529815250432}
11-30 14:31:23.022 5380-5380/net.droid5d.myapp E/ViewRootImpl: sendUserActionEvent() returned.
11-30 14:31:23.039 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=80
11-30 14:31:23.040 5380-5380/net.droid5d.myapp D/ViewRootImpl@89d4f57[Toast]: setView = android.widget.LinearLayout{d717a44 V.E...... ......I. 0,0-0,0} touchMode=true
11-30 14:31:23.040 5380-5380/net.droid5d.myapp V/Toast: Text: Frro in android.widget.Toast$TN@129f72d
11-30 14:31:23.045 5380-5380/net.droid5d.myapp D/ViewRootImpl@89d4f57[Toast]: dispatchAttachedToWindow
11-30 14:31:23.072 5380-5380/net.droid5d.myapp V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
11-30 14:31:23.073 5380-5380/net.droid5d.myapp D/ViewRootImpl@89d4f57[Toast]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[443,2407][996,2568] result=0x27 surface={isValid=true 529685839872} surfaceGenerationChanged=true
11-30 14:31:23.075 5380-5380/net.droid5d.myapp D/ViewRootImpl@89d4f57[Toast]: MSG_RESIZED_REPORT: frame=Rect(443, 2407 - 996, 2568) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=١
11-30 14:31:25.028 5380-5380/net.droid5d.myapp D/ViewRootImpl@89d4f57[Toast]: dispatchDetachedFromWindow
11-30 14:31:25.040 5380-5380/net.droid5d.myapp D/InputTransport: Input channel destroyed: fd=80
11-30 14:31:25.040 5380-5380/net.droid5d.myapp W/SemDesktopModeManager: unregisterListener: Listener is null
11-30 14:31:55.035 5380-5380/net.droid5d.myapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@1fc2462 nm : net.droid5d.myapp ic=null
11-30 14:31:55.035 5380-5380/net.droid5d.myapp I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
11-30 14:31:55.038 5380-5380/net.droid5d.myapp D/InputTransport: Input channel constructed: fd=80
11-30 14:31:55.039 5380-5380/net.droid5d.myapp D/InputTransport: Input channel destroyed: fd=81
11-30 14:32:00.732 5380-5380/net.droid5d.myapp D/ViewRootImpl@65a66bb[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0

1 个答案:

答案 0 :(得分:0)

MainActivity: Update Error: file:///storage/emulated/0/Download/app-debug.apk 
  exposed beyond app through Intent.getData()

这是在Android 7+上吗?

如果有,请谷歌查看FileUriExposedException并使用FileProvider