0
I’m having trouble opening the shared file via Intent
.
I can properly receive the path to the file but mine app closes the execution.
And unfortunately I’m 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 standard 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 other source would just a tap 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, typing error:
Fis = new Fileinputstream(arqUri.getPath());
But now I’m trying file error does not exist.
Edited: I managed to solve the second part of my problem. That block was missing:
<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 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;
}
}
Don’t forget that in order to share a file between applications it must be in a place(path) that can be accessed by both applications.
– ramaral
The file is in the Phone Downloads folder.
– Artur Gaspar
What is the result of
arqUri.getPath()
?– ramaral
/sdcard/Download/a. rlc
– Artur Gaspar
Error return: Filenotfoundexception:/sdcard/Download/maximo.rlc: open failed: EACCES (Permission denied)
– Artur Gaspar
I found the answer here: http://stackoverflow.com/questions/23527767/open-failed-eacces-permission-denied
– Artur Gaspar
The thing is that in API23 version or more not enough of the statements made in the manifest.
– Artur Gaspar
If you had given this information (Permission denied and Api23) it would have been possible to answer.
– ramaral
My fault, no doubt. And I just have to thank the help. Now I can open the file when it is saved. But I have another error when it is an email attachment: android open failed: ENOENT (No such file or directory)
– Artur Gaspar
It is better to ask another question focused only on this problem.
– ramaral