GithubHelp home page GithubHelp logo

Comments (12)

ammarahm-ed avatar ammarahm-ed commented on July 28, 2024

Use createDocument for one time download to a folder.

You can return if the download is cancelled? I don't see the download related code here? Another way would be to delete the file if it was written possibly. Otherwise do nothing.

from react-native-scoped-storage.

projectnetworld avatar projectnetworld commented on July 28, 2024

Use createDocument for one time download to a folder.

You can return if the download is cancelled? I don't see the download related code here? Another way would be to delete the file if it was written possibly. Otherwise do nothing.

Hi Thx for the reply also thx for suggestion the createDocument but the problem exists also with this function
here is what is missing in such function please have a look you
the problem is that you just assume the ideal scenario and not thinking if something go wrong to reject the promise such as user just navigate back to the app with back button
https://github.com/ammarahm-ed/react-native-scoped-storage/blob/d5c1d75312f84915a096d5b665490aa762a3e905/android/src/main/java/com/ammarahmed/scopedstorage/RNScopedStorageModule.java

    public void createDocument(final String fileName, final String mimeType, final String data, final String encoding, final Promise promise) {
        try {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_TITLE, fileName);
        intent.setType(mimeType);

        if (activityEventListener != null) {
            reactContext.removeActivityEventListener(activityEventListener);
            activityEventListener = null;
        }
        activityEventListener = new ActivityEventListener() {
            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
                if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                    if (intent != null) {
                        Uri uri = intent.getData();

                        DocumentFile dir = DocumentFile.fromSingleUri(reactContext,uri);
                        try {
                            byte[] bytes = stringToBytes(data, encoding);
                            OutputStream os = reactContext.getContentResolver().openOutputStream(uri);
                            try {
                                os.write(bytes);
                            } finally {
                                os.close();
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        WritableMap params = Arguments.createMap();
                        params.putString("uri", dir.getUri().toString());
                        params.putString("name", dir.getName());
                        params.putString("type", dir.isDirectory() ? "directory" : "file");
                        if (dir.isFile()) {
                            params.putString("mime", dir.getType());
                        }
                        params.putDouble("lastModified", dir.lastModified());
                        promise.resolve(params);
                    }
                /*MISSING*/


                else{
                  promise.reject();
                }


                /*END*/
                }


                /*MISSING*/


                else{
                  promise.reject();
                }


                /*END*/


                reactContext.removeActivityEventListener(activityEventListener);
                activityEventListener = null;
            }

from react-native-scoped-storage.

ammarahm-ed avatar ammarahm-ed commented on July 28, 2024

Nothing will happen if the user navigates back in the app after selecting the folder. The code will execute and complete. I don't see any problem. It's working great in a production app for me.

from react-native-scoped-storage.

projectnetworld avatar projectnetworld commented on July 28, 2024

Nothing will happen if the user navigates back in the app after selecting the folder. The code will execute and complete. I don't see any problem. It's working great in a production app for me.

yes exactly not after selecting a folder I mean before selection

  • user starts to download
  • after download the file tree opens (createDocument )
  • the user now must select a folder to put the file in ( createDocument )
  • here the user would not select any folder but only press the nav back button until the root folder and then form the document tree to app and the app is still waiting for state for the user to select a folder to put the file

this problem can be resolved by adding the else I mentioned

from react-native-scoped-storage.

ammarahm-ed avatar ammarahm-ed commented on July 28, 2024

Nothing will happen if the user navigates back in the app after selecting the folder. The code will execute and complete. I don't see any problem. It's working great in a production app for me.

yes exactly not after selecting a folder I mean before selection

  • user starts to download
  • after download the file tree opens (createDocument )
  • the user now must select a folder to put the file in ( createDocument )
  • here the user would not select any folder but only press the nav back button until the root folder and then form the document tree to app and the app is still waiting for the user to select a folder to put the file

this problem can be resolved by adding the else i mentioned

It is already handled. It will return null if there is no folder selected.

from react-native-scoped-storage.

projectnetworld avatar projectnetworld commented on July 28, 2024

Nothing will happen if the user navigates back in the app after selecting the folder. The code will execute and complete. I don't see any problem. It's working great in a production app for me.

yes exactly not after selecting a folder I mean before selection

  • user starts to download
  • after download the file tree opens (createDocument )
  • the user now must select a folder to put the file in ( createDocument )
  • here the user would not select any folder but only press the nav back button until the root folder and then form the document tree to app and the app is still waiting for the user to select a folder to put the file

this problem can be resolved by adding the else i mentioned

It is already handled. It will return null if there is no folder selected.

here is the control line

        const CreatedFile = await createDocument(filename, '*/*', buff, 'base64');
        console.log(CreatedFile);

the line console.log(CreatedFile); would never be invoked/executed

from react-native-scoped-storage.

projectnetworld avatar projectnetworld commented on July 28, 2024

Nothing will happen if the user navigates back in the app after selecting the folder. The code will execute and complete. I don't see any problem. It's working great in a production app for me.

yes exactly not after selecting a folder I mean before selection

  • user starts to download
  • after download the file tree opens (createDocument )
  • the user now must select a folder to put the file in ( createDocument )
  • here the user would not select any folder but only press the nav back button until the root folder and then form the document tree to app and the app is still waiting for the user to select a folder to put the file

this problem can be resolved by adding the else i mentioned

It is already handled. It will return null if there is no folder selected.

Hi Did you try the code i sent ?

from react-native-scoped-storage.

ammarahm-ed avatar ammarahm-ed commented on July 28, 2024

@projectnetworld Can you try the master branch and see if it is working as expected now?

yarn add https://github.com/ammarahm-ed/react-native-scoped-storage.git

from react-native-scoped-storage.

projectnetworld avatar projectnetworld commented on July 28, 2024

@projectnetworld Can you try the master branch and see if it is working as expected now?

yarn add https://github.com/ammarahm-ed/react-native-scoped-storage.git

Hi
yes it works as expected now ,the null is now returned thx for the fix

from react-native-scoped-storage.

gkasireddy202 avatar gkasireddy202 commented on July 28, 2024

How to create directory without using openDocumentTree() method and also how to get path for create directory.

from react-native-scoped-storage.

ammarahm-ed avatar ammarahm-ed commented on July 28, 2024

@gkasireddy202 You can't. that's how scoped storage works. You can't just access stuff without acquiring permissions first. To get path for a directory, use openDocumentTree

from react-native-scoped-storage.

gkasireddy202 avatar gkasireddy202 commented on July 28, 2024

I am using below code for acquiring permissions but below error is occurred.
Can you please share example code for acquiring permissions and create directory.

'/storage/emulated/0'does not have permission to create directories - This error is occurred by using below code for creating the directory in external storage.

createDirectory=async()=>{

const path = ${RNFS.ExternalStorageDirectoryPath};
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE );
if (granted == PermissionsAndroid.RESULTS.GRANTED)
{
ScopedStorage.createDirectory(path, "TEST").then((obj) => {
alert(obj)
})
.catch(error => {
console.log("ERROR" + error)
})

    } else {
        console.log('permission denied');
    }
} catch (err) {
    console.warn(err);
}

}

from react-native-scoped-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.