Problem when receiving, via Intent, a file shared by another app

Asked

Viewed 199 times

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.

  • The file is in the Phone Downloads folder.

  • What is the result of arqUri.getPath()?

  • /sdcard/Download/a. rlc

  • Error return: Filenotfoundexception:/sdcard/Download/maximo.rlc: open failed: EACCES (Permission denied)

  • I found the answer here: http://stackoverflow.com/questions/23527767/open-failed-eacces-permission-denied

  • The thing is that in API23 version or more not enough of the statements made in the manifest.

  • If you had given this information (Permission denied and Api23) it would have been possible to answer.

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

  • It is better to ask another question focused only on this problem.

Show 5 more comments

1 answer

0

In this code exposed it seems that is missing, at least, the request for reading in the storage of the device, because otherwise will always occur the permission error. Following is suggested correction:

// Storage Permissions variables
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

    //persmission method.
     public static void verifyStoragePermissions(Activity activity) {
        // Check if we have read or write permission
        int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

        if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

Solution found in: https://stackoverflow.com/questions/33030933/android-6-0-open-failed-eacces-permission-denied

Browser other questions tagged

You are not signed in. Login or sign up in order to post.