简体   繁体   中英

FileNotFoundException in onActivityResult

I am trying to browse only two file-types: images or pdf.

Here is the source:

String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
            myPermissions =new MyPermissions(TestDialog.this, 0, permissions);
            MyPermissions.EventHandler permHandler = new MyPermissions.EventHandler() {
                @Override
                public void handle() {

                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.setType("application/pdf");
                    intent.setType("image/jpeg");
                    startActivityForResult(intent, 0);
                }
            };

            myPermissions.doIfHasPermissions(permHandler);

Here is a my onActivityResult source:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        String url = data.getData().getPath();
        File myFile = new File(url);
        Log.e("base64 ", getStringFile(myFile));


    }
    super.onActivityResult(requestCode, resultCode, data);
}

public String getStringFile(File f) {
    InputStream inputStream = null;
    String encodedFile = "", lastVal;
    try {
        inputStream = new FileInputStream(f.getAbsolutePath());

        byte[] buffer = new byte[10240];//specify the size to allow
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output64.write(buffer, 0, bytesRead);
        }
        output64.close();
        encodedFile = output.toString();
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    lastVal = encodedFile;
    return lastVal;
}

I would like to convert the selected file to Base64, but I get a FileNotFoundException . Does anyone have any idea what am I doing wrong?

I try to browse only two type files,images or pdf

Your code has nothing much to do with files. It uses ACTION_GET_CONTENT , which allows the user to choose a piece of content.

String url = data.getData().getPath();

This line is useless, unless the Uri has a scheme of file . Most likely, it has a scheme of content .

Stop using File and FileInputStream . Instead, get an InputStream from a ContentResolver (from getContentResolver() ) and its openInputStream() method. You can pass in the Uri , and you will get an InputStream regardless of whether the Uri scheme is file or content .

Also note that your app is likely to crash with an OutOfMemoryError , except for fairly small files, as you will not have enough heap space to perform this conversion.

Have a look at

Uri uri = data.getData(); 

Then try to log the value of uri.toString().

You will see that it starts with "content//....".

Do not try to find a file.

Instead of a FileInputStream use an InputStream.

InputStream inputStream = getContentResolver().openInputStream(uri);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM