react-native-image-picker 運用launchCamera直接調取攝像頭的缺陷及修復


  在前幾天用react-native進行android版本開發當中,用到了“react-native-image-picker”的插件;根據業務的需求:點擊按鈕-->直接調取攝像頭進入拍照頁面-->拍照-->保存;也就是直接調取launchCamera,在實際操作中,調取攝像頭拍照&獲取圖片都沒有問題,但是當再次點擊拍照按鈕進入拍照頁面,進行“拍照不保存退出、OR進入拍照頁不拍照退出”這樣的操作時,會將第一次拍的照片文件損毀,也就是在頁面內有如下狀況:

 

點開查看大圖,也都是裂開的圖片;在需求急需解決的狀況下,我分析了該問題:

  Q1、該照片被刪除,但是本地相冊未及時刷新;

  Q2、該圖片未被刪除,只是被暫時的損毀掉了;

鑒於以上兩種可能,我進行的對應操作是:

  方法1:(對應Q1)在再次點擊拍照按鈕時,進行本地文件的實際刪除操作,也就是刪除照片實體文件;(但是由於android功力不夠,該方法並未成功

  code(刪除):

 1 @ReactMethod
 2     private void deleteImage(String fileName, String uri, Callback callback) {
 3         try {
 4             File file = new File(fileName);
 5             if (file.exists())
 6                 file.delete();
 7             ContentResolver mContentResolver = this.getContentResolver();
 8             String where = MediaStore.Images.Media.DATA + "='" + fileName + "'";
 9             // 刪除操作
10             mContentResolver.delete(Uri.parse(uri), where, null);
11             //刷新操作
12             File delete_file = new File(uri);
13             // 掃描單個媒體文件,注意是文件,不是文件夾
14             new SingleMediaScanner(this, delete_file);
15             callback.invoke("刪除成功");
16 //                    else
17 //            callback.invoke('刪除失敗');
18 
19         } catch (Exception e) {
20         }
21 
22     }

code:(刷新)

 1 public class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {
 2 
 3     private MediaScannerConnection mMs;
 4     private File mFile;
 5 
 6     public SingleMediaScanner(Context context, File f) {
 7         mFile = f;
 8         mMs = new MediaScannerConnection(context, this);
 9         mMs.connect();
10     }
11 
12     @Override
13     public void onMediaScannerConnected() {
14         mMs.scanFile(mFile.getAbsolutePath(), null);
15     }
16 
17     @Override
18     public void onScanCompleted(String path, Uri uri) {
19         mMs.disconnect();
20     }
21 
22 }

 

  方法2:(對應Q2)對launchCamera的源碼進行調整,如下,

code(修改插件react-native-image-picker源碼)

 1 // NOTE: Currently not reentrant / doesn't support concurrent requests
 2   @ReactMethod
 3   public void launchCamera(final ReadableMap options, final Callback callback)
 4   {
 5     //新加begin
 6     Activity currentActivity = getCurrentActivity();
 7 
 8     if (currentActivity == null)
 9     {
10       responseHelper.invokeError(callback, "can't find current Activity");
11       return;
12     }
13     //新加end
14 
15     if (!isCameraAvailable())
16     {
17       responseHelper.invokeError(callback, "Camera not available");
18       return;
19     }
20 //    注釋掉原有的代碼
21 //    final Activity currentActivity = getCurrentActivity();
22 //    if (currentActivity == null)
23 //    {
24 //      responseHelper.invokeError(callback, "can't find current Activity");
25 //      return;
26 //    }
27 
28     this.options = options;
29     //新加begin
30     imageConfig = new ImageConfig(null, null, 0, 0, 100, 0, false);
31     //新加end
32     if (!permissionsCheck(currentActivity, callback, REQUEST_PERMISSIONS_FOR_CAMERA))
33     {
34       return;
35     }
36 
37     parseOptions(this.options);
38 
39     int requestCode;
40     Intent cameraIntent;
41 
42     if (pickVideo)
43     {
44       requestCode = REQUEST_LAUNCH_VIDEO_CAPTURE;
45       cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
46       cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, videoQuality);
47       if (videoDurationLimit > 0)
48       {
49         cameraIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, videoDurationLimit);
50       }
51     }
52     else
53     {
54       requestCode = REQUEST_LAUNCH_IMAGE_CAPTURE;
55       cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
56 
57       final File original = createNewFile(reactContext, this.options, false);
58       imageConfig = imageConfig.withOriginalFile(original);
59 
60       cameraCaptureURI = RealPathUtil.compatUriFromFile(reactContext, imageConfig.original);
61       if (cameraCaptureURI == null)
62       {
63         responseHelper.invokeError(callback, "Couldn't get file path for photo");
64         return;
65       }
66       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraCaptureURI);
67     }
68 
69     if (cameraIntent.resolveActivity(reactContext.getPackageManager()) == null)
70     {
71       responseHelper.invokeError(callback, "Cannot launch camera");
72       return;
73     }
74 
75     this.callback = callback;
76 
77     try
78     {
79       currentActivity.startActivityForResult(cameraIntent, requestCode);
80     }
81     catch (ActivityNotFoundException e)
82     {
83       e.printStackTrace();
84       responseHelper.invokeError(callback, "Cannot launch camera");
85     }
86   }

如此,便完成了對該操作的問題修復;

(備注:第一種的解決方案可行,但是開發有誤,如果哪位同學有好的想法,還請告知;對於react-native-image-picker插件的相關問題,哪位同學還有其他好的開發經驗,可以隨時分享討論:【qq 930369018】)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM