aliyun / aliyun-oss-react-native

MIT License
148 stars 99 forks source link

asyncUpload method can't find the file path #83

Open LoverFancy opened 3 years ago

LoverFancy commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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!