Android6.0自带文件管理器无法打开apk

Android从6.0开始在设置中自带了一个文件浏览器,在6.0之前系统是不自带的,但是这个文件管理器不能打开apk文件,不能安装app。那是因为代码里面没有对APK文件做识别处理,下面这个补丁可以帮你搞定.

diff --git a/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java b/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
index a57bcc6..1c41a78 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
@@ -27,6 +27,9 @@ import static com.android.documentsui.DirectoryFragment.ANIM_DOWN;
 import static com.android.documentsui.DirectoryFragment.ANIM_NONE;
 import static com.android.documentsui.DirectoryFragment.ANIM_UP;
 
+
+import java.io.File;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -642,12 +645,23 @@ public class DocumentsActivity extends BaseActivity {
             // Go straight to viewing
             final Intent view = new Intent(Intent.ACTION_VIEW);
             view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-            view.setData(doc.derivedUri);
-
+           // view.setData(doc.derivedUri);
+            
+	    String path = doc.derivedUri.toString();
+            if(path.endsWith(".apk")){
+                String path1 = doc.documentId;
+                String path2 =path1.replace(":","/");
+                File file =new File("storage/"+path2);
+                 
+		view.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
+            }else{
+
+	     	view.setData(doc.derivedUri);
+	    }
             try {
                 startActivity(view);
             } catch (ActivityNotFoundException ex) {
-                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
+		Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
             }
         }
     }


你可能感兴趣的:(Android6.0自带文件管理器无法打开apk)