2016-02-26 96 views
0

首先,如果您能帮助我,我将非常感谢您。从Android上的文件加载.json文件时出现的问题

在我的项目中,我必须在应用程序中上传文件.json,然后从浏览器中取出它。

我处理它从的Manifest.xml与意图过滤器如下:

<intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="file" android:host="*" android:pathPattern=".*\\.json" android:mimeType="*/*" /> 
    </intent-filter> 

然后,从MainActivity.java,我构建File对象,并从创建的文件中的字符串结果:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent intent = getIntent(); 
     String action = intent.getAction(); 
     Uri uri = intent.getData(); 

     if (action != null){ 
      if (action.compareTo(Intent.ACTION_VIEW) == 0) { 
       String scheme = intent.getScheme(); 
       String path = intent.getDataString();     

       if (scheme.compareTo(ContentResolver.SCHEME_FILE) == 0) { 

        try { 
         InputStream inputStream = getContentResolver().openInputStream(uri); 
         System.out.println("InputStream loaded"); 
        } catch (FileNotFoundException e) { 
         System.out.println("InputStream not loaded"); 
         e.printStackTrace(); 
        }  
       } 
      } 
     } 
    } 

字符串对象的值路径文件:///storage/emulated/0/Download/prova.json

主要问题是,当我尝试加载文件时,它会抛出一个异常,指出该文件不存在,即使它确实如此。

再次谢谢大家!

+0

始终发布堆栈跟踪。 –

回答

0

所以,你想读的JSON是在设备存储,以便在你的清单,你需要启用以下权限

这将使旅游应用读取aplication之外的设备上的文件上下文

+0

当我添加权限时,代码开始工作!谢谢! –

+0

请接受答案 –

相关问题