Skip to content

asyncUpload method can't find the file path #83

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

Open
LoverFancy opened this issue Oct 27, 2020 · 3 comments
Open

asyncUpload method can't find the file path #83

LoverFancy opened this issue Oct 27, 2020 · 3 comments

Comments

@LoverFancy
Copy link

@luozhang002

when I use asyncUpload method to upload image to oss, I get the below error:

err Error: /storage/emulated/0/c9ce6f70-f5cf-4086-aac6-2023d39c5986.jpg: open failed: ENOENT (No such file or directory)
[ErrorMessage]: /storage/emulated/0/c9ce6f70-f5cf-4086-aac6-2023d39c5986.jpg: open failed: ENOENT (No such file or directory)
at Object.fn [as asyncUpload] (NativeModules.js:99)
at Object.asyncUpload (index.js:74)
at _callee2$ (VM5 face_detection_sign.bundle:169)
at tryCatch (runtime.js:63)
at Generator.invoke [as _invoke] (runtime.js:293)
at Generator.next (runtime.js:118)
at tryCatch (runtime.js:63)
at invoke (runtime.js:154)
at runtime.js:164
at tryCallOne (core.js:37)

my local image uri: "file:///data/user/0/com.mysoft.yunke.msdjdev/cache/Camera/c9ce6f70-f5cf-4086-aac6-2023d39c5986.jpg"

why can't find the image file and oss read the file path maybe wrong.
I can find the file in android studio

info:

android 10
react-native 0.61
aliyun-oss-react-native latest version

@Shuo-Mr
Copy link

Shuo-Mr commented Jan 7, 2021

I also have this problem, saying that there is no such file, but the path of the error prompt is not my path at all

@Shuo-Mr
Copy link

Shuo-Mr commented Jan 13, 2021

I tried to look at the logic of the source code and found the wrong location,but I didn't go into the reasons.

The method is as follows:

` public void asyncUpload(final ReactContext context, String bucketName, String ossFile, String sourceFile, ReadableMap options, final Promise promise) {
// Content to file:// start
Uri selectedVideoUri = Uri.parse(sourceFile);
// 1. content uri -> file path
// 2. inputstream -> temp file path
Cursor cursor = null;

try {
    String[] proj = {MediaStore.Images.Media.DATA};

   // Here, the initialization fails, causing the getcolumnindexorthrow below to throw an exception
    cursor = context.getCurrentActivity().getContentResolver().query(selectedVideoUri, proj, null, null, null);



    if (cursor == null) 
        sourceFile = selectedVideoUri.getPath();
    }

    // error: cursor is null。The real reason for the path change is not in this, but in the exception handling
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();
    sourceFile = cursor.getString(column_index);
} catch (Exception e) {

       // After the processing of the following method, the path has changed
      // sourceFile = FileUtils.getFilePathFromURI(context.getCurrentActivity(), selectedVideoUri);

} finally {
    if (cursor != null) {
        cursor.close();
    }
}
// init upload request
PutObjectRequest put = new PutObjectRequest(bucketName, ossFile, sourceFile);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/octet-stream");
put.setMetadata(metadata);



//
.....

//
// set callback
Log.d("AliyunOSS", "OSS uploadObjectAsync ok!");
}
`
After the annotation, the path is normal and the upload is successful. I think the method is to copy the file and then return a new file. I don't know whether it is for compression. But what happened was that the file couldn't be found.
// sourceFile = FileUtils.getFilePathFromURI(context.getCurrentActivity(), selectedVideoUri);

@Leo2018Wu
Copy link

I tried to look at the logic of the source code and found the wrong location,but I didn't go into the reasons.

The method is as follows:

` public void asyncUpload(final ReactContext context, String bucketName, String ossFile, String sourceFile, ReadableMap options, final Promise promise) {
// Content to file:// start
Uri selectedVideoUri = Uri.parse(sourceFile);
// 1. content uri -> file path
// 2. inputstream -> temp file path
Cursor cursor = null;

try {
    String[] proj = {MediaStore.Images.Media.DATA};

   // Here, the initialization fails, causing the getcolumnindexorthrow below to throw an exception
    cursor = context.getCurrentActivity().getContentResolver().query(selectedVideoUri, proj, null, null, null);



    if (cursor == null) 
        sourceFile = selectedVideoUri.getPath();
    }

    // error: cursor is null。The real reason for the path change is not in this, but in the exception handling
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();
    sourceFile = cursor.getString(column_index);
} catch (Exception e) {

       // After the processing of the following method, the path has changed
      // sourceFile = FileUtils.getFilePathFromURI(context.getCurrentActivity(), selectedVideoUri);

} finally {
    if (cursor != null) {
        cursor.close();
    }
}
// init upload request
PutObjectRequest put = new PutObjectRequest(bucketName, ossFile, sourceFile);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/octet-stream");
put.setMetadata(metadata);



//
.....

//
// set callback
Log.d("AliyunOSS", "OSS uploadObjectAsync ok!");
}
`
After the annotation, the path is normal and the upload is successful. I think the method is to copy the file and then return a new file. I don't know whether it is for compression. But what happened was that the file couldn't be found.
// sourceFile = FileUtils.getFilePathFromURI(context.getCurrentActivity(), selectedVideoUri);

I have try this way and it's work!

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

3 participants