在做weex開發時使用leancloud檔案上傳

來源:互聯網
上載者:User

標籤:leancloud   android   weex   

 不同於移動端原生開發,當開發人員使用weex移動開發時,使用第三方SDK比較棘手。因為第三方的JS SDK是無法直接拿來使用的,環境不同。必須使用原生SDK,下面我介紹一下自己是如何在weex開發時(安卓)使用leancloud的服務的。


首先去leancloud的安卓SDK下載頁面,下載SDK,這裡我只使用了最基礎的SDK包

<img src="/uploads/default/original/2X/5/582ffee695e0192ae085f0bc0e033543b316f673.png" width="452" height="221"> 

其實用Gradle也可以啊,我喜歡下載手動整合。

然後在mainApplication.java檔案中onCreat()方法裡初始化


    AVOSCloud.initialize(this,yourappid,yourappkey)   

做過weex開發都知道,這個方法裡面還會初始化WXSDKEngine和ImageAdapter,它們不衝突。

 


    InitConfig config = new InitConfig.Builder().setImgAdapter(new ImageAdapter()).build();

    WXSDKEngine.initialize(this,config);

現在進入正題了,如何在weex頁面實現上傳圖片呢?

Step.1  擴充一個Module,調用手機系統的圖庫


    @WXModuleAnno(runOnUIThread = true)

    public void uploadimage(){

        int REQUESTCODE_PICK = 0;

        Intent intent;

        if(Build.VERSION.SDK_INT < 19){

            Log.d("chenlei api level","Your api is lower than 19");

            intent = new Intent(Intent.ACTION_GET_CONTENT);

            intent.setType("image/*");

        }else{

            Log.d("chenlei api level","Your api is higher than 19");

            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        }

        ((Activity)mWXSDKInstance.getContext()).startActivityForResult(intent,REQUESTCODE_PICK);

    }


Step.2 在負責渲染weex頁面的activity類裡,重寫onActivityResult方法


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Toast.makeText(getApplicationContext(),"這個函數有效嗎?",Toast.LENGTH_SHORT);

        super.onActivityResult(requestCode, resultCode, data);

        String s = String.valueOf(resultCode);

        if(resultCode == Activity.RESULT_OK && requestCode ==  REQUESTCODE_PICK){

            Uri uri = data.getData();

            Cursor cursor = getContentResolver().query(uri, null, null, null,null);

            if (cursor != null && cursor.moveToFirst()) {

                Path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

                Log.d("Path", Path);

            }

            try {

                final AVFile file = AVFile.withAbsoluteLocalPath("test.png",Path);

                file.saveInBackground(new SaveCallback() {

                    @Override

                    public void done(AVException e) {

                        Log.d(TAG, file.getUrl());

                        WXBridgeManager manager = WXBridgeManager.getInstance();

                        Map<String,String> urlData = new HashMap<String, String>();

                        urlData.put("url",file.getUrl());

                        urlData.put("showimage","true");

                        urlData.put("uploadhint","改變圖片");

                        urlData.put("showdelete","true");

                        String jsonStr = JSON.toJSONString(urlData);

                        WXRefreshData refreshData = new WXRefreshData(jsonStr,false);

                        manager.refreshInstance(mInstance.getInstanceId(),refreshData);

                    }

                }, new ProgressCallback() {

                    @Override

                    public void done(Integer integer) {

                        Toast.makeText(getApplicationContext(),"已經上傳"+String.valueOf(integer)+"%",Toast.LENGTH_SHORT).show();

                    }

                });

            }catch(Exception e){

                Toast.makeText(getApplicationContext(),"上傳圖片失敗",Toast.LENGTH_SHORT).show();

            }

        }else {

            Toast.makeText(getApplicationContext(),"請選擇圖片",Toast.LENGTH_SHORT).show();

        }

    }


小提示:我們可以在AVFile的回呼函數中建立WXBridgeManager擷取當前執行個體,擷取檔案上傳的進度和最終URL,來更新頁面組件狀態和資訊。

簡單幾步,就實現了leancloud在weex開發中的使用,感謝leancloud提供的優秀資源,你們是開發人員的福音呀。

更多源碼可以去github上查看,歡迎拍磚和star。

<img src="/uploads/default/original/2X/1/1ab589f1125cb3893ff1d0dbe7c9cc5b8d2f77d7.png" width="466" height="500"> 

項目地址----------->[leancloud在weex開發中的使用項目執行個體][1]



  [1]: https://github.com/iChenLei/weex-android-joke

有任何疑問可以聯絡我的Email [email protected],交流weex和leancloud開發。


在做weex開發時使用leancloud檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.