在我的Android 6.0.1应用程序中,我有一个针对我的应用程序生成的自定义文件的intent-filter,但intent-filter只能在我重新启动之前运行。重启后,我收到消息:无法打开文件。
在我的Android 4.1.2设备中,我有相同的应用程序和intent-filter,但重启后仍然可以访问我的文件。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gray10.glassshutters">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".MCRViewer"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.mcr" />
<data android:host="*" />
</intent-filter>
</activity>
</application>
以下是处理意图的代码:
public class MCRViewer extends Activity
{
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MCRViewer.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MCRViewer.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MCRViewer.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
setContentView(R.layout.scrolltextview);
TextView text = (TextView) findViewById(R.id.TEXT_ID);
//text.setText("Text Changed");
Intent intent = getIntent();
String name = intent.getData().getPath();
FileReader fr = null;
BufferedReader br = null;
try
{
fr = new FileReader(name);
br = new BufferedReader(fr);
String line = br.readLine();
while (null != line)
{
text.append(line);
text.append("\n");
line = br.readLine();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (null != br)
{
try
{
br.close();
}
catch (IOException e1)
{
// ignore
}
}
if (null != fr)
{
try
{
fr.close();
}
catch (IOException e)
{
// ignore
}
}
}
}
}
任何帮助都将不胜感激。
经过一些实验,似乎问题出在三星的My Files应用程序上。另一种文件浏览器工作正常。