Android mhtml文件未在棒棒糖和棉花糖的webview中正确加载/显示,而是在棒棒糖前设备上正确加载/显示

Android mhtml文件未在棒棒糖和棉花糖的webview中正确加载/显示,而是在棒棒糖前设备上正确加载/显示,android,webview,android-5.0-lollipop,android-6.0-marshmallow,mhtml,Android,Webview,Android 5.0 Lollipop,Android 6.0 Marshmallow,Mhtml,我的数据库中有一个.mhtml文件作为字节数组。我已经写了下面的代码,它在棒棒糖前的设备中运行良好。但它对棒棒糖和棉花糖不起作用 代码:- String path = filepath.getAbsolutePath()+"/StudyWellAppv5/"+fname; byte[] bytes = tblK.gethtmlFile(); try { if (!file.exists()) { fil

我的数据库中有一个.mhtml文件作为字节数组。我已经写了下面的代码,它在棒棒糖前的设备中运行良好。但它对棒棒糖和棉花糖不起作用

代码:-

       String path = filepath.getAbsolutePath()+"/StudyWellAppv5/"+fname;
       byte[] bytes = tblK.gethtmlFile();
       try {
           if (!file.exists()) {
               file.createNewFile();
           }
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(bytes);
           webview.loadUrl("file://" + path);
          }
          catch(Exception e){ e.printStackTrace();}
/Permission to read and write from external storage
   private static final int REQUEST_EXTERNAL_STORAGE = 1;
   private static String[] PERMISSIONS_STORAGE = {
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.WRITE_EXTERNAL_STORAGE
   };


int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
       int readpermission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
       if (permission != PackageManager.PERMISSION_GRANTED||readpermission != PackageManager.PERMISSION_GRANTED) {
           // We don't have permission so prompt the user
           ActivityCompat.requestPermissions(
                   getActivity(),
                   PERMISSIONS_STORAGE,
                   REQUEST_EXTERNAL_STORAGE
           );
       }
"Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.571 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(1618)] "Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.598 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image002.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image004.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image006.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.671 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image008.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:49:10.182 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
运行时权限代码:-

       String path = filepath.getAbsolutePath()+"/StudyWellAppv5/"+fname;
       byte[] bytes = tblK.gethtmlFile();
       try {
           if (!file.exists()) {
               file.createNewFile();
           }
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(bytes);
           webview.loadUrl("file://" + path);
          }
          catch(Exception e){ e.printStackTrace();}
/Permission to read and write from external storage
   private static final int REQUEST_EXTERNAL_STORAGE = 1;
   private static String[] PERMISSIONS_STORAGE = {
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.WRITE_EXTERNAL_STORAGE
   };


int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
       int readpermission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
       if (permission != PackageManager.PERMISSION_GRANTED||readpermission != PackageManager.PERMISSION_GRANTED) {
           // We don't have permission so prompt the user
           ActivityCompat.requestPermissions(
                   getActivity(),
                   PERMISSIONS_STORAGE,
                   REQUEST_EXTERNAL_STORAGE
           );
       }
"Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.571 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(1618)] "Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.598 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image002.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image004.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image006.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.671 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image008.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:49:10.182 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
错误日志:-

       String path = filepath.getAbsolutePath()+"/StudyWellAppv5/"+fname;
       byte[] bytes = tblK.gethtmlFile();
       try {
           if (!file.exists()) {
               file.createNewFile();
           }
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(bytes);
           webview.loadUrl("file://" + path);
          }
          catch(Exception e){ e.printStackTrace();}
/Permission to read and write from external storage
   private static final int REQUEST_EXTERNAL_STORAGE = 1;
   private static String[] PERMISSIONS_STORAGE = {
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.WRITE_EXTERNAL_STORAGE
   };


int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
       int readpermission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
       if (permission != PackageManager.PERMISSION_GRANTED||readpermission != PackageManager.PERMISSION_GRANTED) {
           // We don't have permission so prompt the user
           ActivityCompat.requestPermissions(
                   getActivity(),
                   PERMISSIONS_STORAGE,
                   REQUEST_EXTERNAL_STORAGE
           );
       }
"Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.571 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(1618)] "Blocked script execution in 'file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (1618)
06-16 13:48:42.598 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image002.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image004.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.599 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image006.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:48:42.671 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///C:/EC2C4CD1/1_files/image008.jpg", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)
06-16 13:49:10.182 1244-1244/com.samji.StudyWellAppv5 I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml", source: file:///storage/emulated/0/StudyWellApp/2016-06-16%2001-48-42.mhtml (0)

注意:-此代码在棒棒糖制作前的设备上运行良好。它不仅适用于棒棒糖和棉花糖。

您必须获得API 23(棉花糖)的运行时权限

示例代码:

 public class MainActivity extends AppCompatActivity {

        private static final int REQUEST_RUNTIME_PERMISSION = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //SomeStuffWhichNeedPermission(); dont call here directly wihtout permission in 6.0 do like below
            checkPremission();
        }

        void SomeStuffWhichNeedPermission() {
            // storing data code here now
            String path = filepath.getAbsolutePath() + "/StudyWellAppv5/" + fname;
            byte[] bytes = tblK.gethtmlFile();
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bytes);
                webview.loadUrl("file://" + path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        void checkPremission() {
            //select which permission you want
            final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
            // if in fragment use getActivity()
            if (ContextCompat.checkSelfPermission(ActivityName.this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(ActivityName.this, permission)) {
                } else {
                    ActivityCompat.requestPermissions(ActivityName.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
                }
            } else {
                // you have permission go ahead
                SomeStuffWhichNeedPermission();
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case REQUEST_RUNTIME_PERMISSION:
                    final int numOfRequest = grantResults.length;
                    final boolean isGranted = numOfRequest == 1
                            && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                    if (isGranted) {
                        // you have permission go ahead
                        SomeStuffWhichNeedPermission();
                    } else {
                        // you dont have permission show toast
                    }
                    break;
                default:
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

你有运行时权限吗?是的,请检查更新的问题。请检查我的更新问题。我已经有运行时权限代码。@Sam添加新代码后,您看到权限对话框了吗?