android - 图片选择器: 高仿微信. PhotoPicker

访问量: 99

参考:

高仿微信:https://github.com/donglua/PhotoPicker (建议使用)

步骤:

1. 安装:

dependencies {
    compile 'me.iwf.photopicker:PhotoPicker:0.9.10@aar'
    
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.github.bumptech.glide:glide:4.1.1'
}

2. 修改AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA" />
  <application
    ...
    >
    ...
    
    <activity android:name="me.iwf.photopicker.PhotoPickerActivity"
      android:theme="@style/Theme.AppCompat.NoActionBar" 
       />

    <activity android:name="me.iwf.photopicker.PhotoPagerActivity"
      android:theme="@style/Theme.AppCompat.NoActionBar"/>
    
  </application>
</manifest>

3. 在对应的Activity中,增加:

    public void chooseImage(View view){
        PhotoPicker.builder()
                .setPhotoCount(9)
                .setShowCamera(true)
                .setShowGif(true)
                .setPreviewEnabled(false)
                .start(UserEditActivity.this, PhotoPicker.REQUEST_CODE);
    }

4. 点击选择或者 拍照后,上传图片:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FILE_CHOOSER_RESULT_CODE) {
            if (null == uploadMessage && null == uploadMessageAboveL) return;
            Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
            if (uploadMessageAboveL != null) {
                onActivityResultAboveL(requestCode, resultCode, data);
            } else if (uploadMessage != null) {
                uploadMessage.onReceiveValue(result);
                uploadMessage = null;
            }
        }


        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                ArrayList photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                for(int i = 0 ; i < photos.size(); i ++){
                    Log.d(TAG, "== photos: " + photos.get(i));

                    Toast.makeText(UserEditActivity.this, "更改头像中, 请稍等...", Toast.LENGTH_SHORT).show();

                    OkHttpClient httpClient = new OkHttpClient();
                    RequestBody requestBody = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("icon", photos.get(i), RequestBody.create(MediaType.parse("image/png"), new File(photos.get(i))))
                            .addFormDataPart("id", getUserId())
                            .build();

                    String url = PageUrls.SERVER_URL +  "/interface/users/update_user_icon_for_android_native";
                    Log.d(TAG, "== url: " + url);
                    Request request = new Request.Builder().
                            url(url)
                            .post(requestBody)
                            .build();

                    httpClient.newCall(request)
                            .enqueue(new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {
                                    e.printStackTrace();
                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    String response_result = response.body().string();
                                    Log.d(TAG, "== response: " + response_result);
                                    try {
                                        JSONObject json = new JSONObject(response_result);
                                        if(json.getString("message").equals("更换成功")){
                                            Message msg = Message.obtain();
                                            msg.what = 1;
                                            msg.obj = json.getString("icon");
                                            handler.sendMessage(msg);
                                        }
                                    } catch (JSONException e) {
                                        Message msg = Message.obtain();
                                        msg.what = 0;
                                        msg.obj = "更换头像失败,请确认网络畅通";
                                        handler.sendMessage(msg);
                                        e.printStackTrace();
                                    }
                                }
                            });

                }
            }
        }

    }






    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Toast.makeText(UserEditActivity.this, "更改头像成功", Toast.LENGTH_SHORT).show();
                    GlideApp.with(UserEditActivity.this).load((String)msg.obj)
                            .fitCenter()
                            .into(avatar);
                    break;
                case 0:
                    Toast.makeText(UserEditActivity.this, "更改头像失败,请确认网络畅通", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

下面是另外两个,仅供参考:https://github.com/luminousman/MultipleImagePick , https://github.com/jkwiecien/EasyImage

EasyImage的用法:

1. 修改AndroidManifest.xml: 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. 添加依赖:build.gradle: 

repositories {

    maven { url 'https://jitpack.io' }
}

dependencies {
    compile 'com.github.jkwiecien:EasyImage:2.0.2'
}

3. 添加对应的方法:

    // 点击 某个按钮后,会触发该方法,会弹出选择相册页面 
    public void chooseImage(View view){
        EasyImage.openChooserWithDocuments(this, "choose image", 0);
    }

4. 还有其他的后续函数.不过看起来这个效果不好.所以建议使用 仿微信的那个.

订阅/RSS Feed

Subscribe

分类/category