Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Sharing the generated image with a share intent #11

Closed
sampurcell93 opened this issue Sep 22, 2016 · 4 comments
Closed

[Android] Sharing the generated image with a share intent #11

sampurcell93 opened this issue Sep 22, 2016 · 4 comments

Comments

@sampurcell93
Copy link

sampurcell93 commented Sep 22, 2016

Hi, maybe this is more a general purpose Android question, but I thought perhaps you could help. Since the uri that is generated for a snapshot only exists in the app cache, passing its uri to a share intent always fails. As soon as I change it to a file saved on external storage, it works. Obviously I'd like to avoid doing this, so I'm looking for other ways to achieve this. More than willing to submit a PR. Thanks!

EDIT: My proposal would be an option like {saveToDisk: true} being passed in which would cause the module to call something like createPermanentFile instead of createTempFile, but my native Android fu is limited, so there's probably a better way.

@gre
Copy link
Owner

gre commented Sep 23, 2016

I'm not sure if this should be in react-native-view-shot that should stay minimal.
So currently, the library will put images in a temporary directory that will be cleanup next time the app is opened. I grab the technical from one of RN API (I think it was on ImageEditor).
The problem is if we start introducing other way is we will still have to handle the cleanup and maybe we can't even know. I think it's your (user of the lib) responsibility to copy the image somewhere else , do some stuff with it, and then remove it when you don't anymore need it. You know the best your lifecycle and we probably can't guess it easily.

@sampurcell93
Copy link
Author

Yeah, that's fair. I'll take a look at doing that, thanks!

@gre gre closed this as completed Sep 26, 2016
@gre
Copy link
Owner

gre commented Oct 3, 2016

@sampurcell93 please see this change: a85cf96 of v 1.3.0

the string returned by the lib used to be like file:/data/user/0/... but is now /data/user/0/....
So in previous version of the lib, new File("file:/...") was not working. It was probably the reason you had an issue (the file is not deleted, it's simply wrong path in the lib), but now it should work.

@gre
Copy link
Owner

gre commented Oct 3, 2016

that said, you probably still will have to copy somewhere else, not because the file is deleted but because the file is in a private place ATM. not accessible from external (so not from an Intent).

    public void copy(File src, File dst) throws IOException {
        FileInputStream inStream = new FileInputStream(src);
        FileOutputStream outStream = new FileOutputStream(dst);
        FileChannel inChannel = inStream.getChannel();
        FileChannel outChannel = outStream.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inStream.close();
        outStream.close();
    }

    public File copyToPublic (File input) throws IOException {
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        dir.mkdirs();
        File out = new File(dir, input.getName());
        copy(input, out);
        return out;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants