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

0

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;
                    }
                }
    
asked by anonymous 11.01.2017 / 12:43

1 answer

0

In this code it appears that the request to read in the device storage is at least missing, otherwise the permission error will occur. Here is a 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 at: link

    
27.04.2018 / 00:54