I'm having trouble opening the shared file via Intent
.
I can correctly receive the path to the file but my app terminates execution.
And unfortunately I am not able to capture the error generated.
My code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d("Artur", "Action: "+action);
Log.d("Artur", "Type: "+type);
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("*/*".equals(type)) {
Uri arqUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (arqUri != null) {
try {
FileInputStream fis;
Log.d("Artur", "vai abrir: "+arqUri.getPath());
fis = openFileInput(arqUri.getPath()); //<----ERRO
Log.d("Artur", "Leu: "+(byte)fis.read());
fis.close();
} catch (FileNotFoundException e) {
Log.d("Artur", "FileNotFoundException");
e.printStackTrace();
return;
} catch (IOException e) {
Log.d("Artur", "IOException");
e.printStackTrace();
return;
}
}
}
} else {
// Handle other intents, such as being started from the home screen
//Toast.makeText(getApplicationContext(),"Arquivo nao identificado", Toast.LENGTH_SHORT).show();
}
I also have another question. I can only receive a file if the "share with" option is used in the calling app. I would like my program to become the default for opening this type of file, which has the RLC extension, for example: "config.rlc". So just set it as default to read this file, when receiving one by email or another source just a touch on it for my program to run. As it is with a text editor, pdf, image viewer and others. Thank you.
Edited.
I solved the opening problem, typo:
fis = new FileInputStream (arqUri.getPath ());
But now I'm trying the file error does not exist.
Edited: I was able to solve the second part of my problem. Missing this block:
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\.rlc" />
</intent-filter>
Now I continue with the file problem does not exist.
Uri arqUri = intent.getData();
Log.d("Artur", "arqUri: " + arqUri);
if (arqUri != null) {
try {
FileInputStream fis;
String caminho = arqUri.getPath();
Log.d("Artur", "Caminho: " + caminho);
File file = new File(caminho);
fis = new FileInputStream(file);
Log.d("Artur", "Leu: " + (byte) fis.read());
fis.close();
} catch (FileNotFoundException e) {
Log.d("Artur", "FileNotFoundException");
e.printStackTrace();
//return;
} catch (IOException e) {
Log.d("Artur", "IOException");
e.printStackTrace();
//return;
}
}