当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Issues loading .json file from file on Android

问题描述:

first I thank you all if you can help me.

In my project, I have to upload a file .json in my application and I take it from a browser.

I handle it from the Manifest.xml with an Intent Filter as follow:

 <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>

Then, from the MainActivity.java, I construct the File object and get the String result from the File created:

 @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();

}

}

}

}

}

The value of the String object path is file:///storage/emulated/0/Download/prova.json.

The main problem is that, when I try to load the file, it throws an exception saying that the file doesn't exist, even if it's really sure it is.

Thank you all again!

网友答案:

So the json you want to read is on the device storage so in your manifest you need to enable the following permissions

this will enable tour app to read files on the device outside of the aplication context

分享给朋友:
您可能感兴趣的文章:
随机阅读: