Android development realizes the function of calling album pictures and cropping and uploading

This function uses Apache's open source project simplecropimage.
The CropImage class in the following code is a class in SimpleCropImage, which needs to be registered in the Manifest. The dependency classes used by simplecropimage are in the attachment.
Go directly to the code:
    /** Open the album (first step)*/
    private static final String IMAGE_UNSPECIFIED = "image/*";

    private void openPhotos() {
        Intent intent = new Intent(Intent.ACTION_PICK);
//        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
        startActivityForResult(intent, REQUEST_PHOTOS);
    }

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case REQUEST_CUTRESULT:
                dealCutResult(resultCode, data);
                break;
            case REQUEST_PHOTOS:
                dealPhotosResult(resultCode, data);
                break;
        }

    private void dealPhotosResult(int resultCode, Intent data) {
        if (Activity.RESULT_OK == resultCode) {
            if (data != null) {
                // Get photo uri from album
                startPhotoCut(data.getData());
            }
        } else {
            if(GlobleParams.isEnglish){
               shortToast("Upload canceled");
            }else{
               shortToast("You have cancelled the upload operation");
            }
        }
    }

    /**Start cropping (second step)*/
    public void startPhotoCut {Uri uri) {
        String realPathFromURI = getRealPathFromURI(uri);
        if(realPathFromURI == null){
            return;
        }
        File file = new File(realPathFromURI);
        Intent intent = new Intent(App.getInstance(), CropImage.class);

        // tell CropImage activity to look for image to crop
        intent.putExtra(CropImage.IMAGE_PATH, file.getAbsolutePath());

        // allow CropImage activity to rescale image
        intent.putExtra(CropImage.SCALE, true);

        // if the aspect ratio is fixed to ratio 3/2
        intent.putExtra(CropImage.ASPECT_X, 0);
        intent.putExtra(CropImage.ASPECT_Y, 0);

        // start activity CropImage with certain request code and listen
        // for result
        startActivityForResult(intent, REQUEST_CUTRESULT);
    }
  private void dealCutResult(int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            String path = data.getStringExtra(CropImage.IMAGE_PATH);
            // if nothing received
            if (path == null) {return;}
            uploadPhoto(path);

        } else {
            if(GlobleParams.isEnglish){
                shortToast("Upload canceled");
            }else{
                shortToast("You have cancelled the upload operation");
            }
        }
    }
    /**Start uploading (step 3)*/
    public void uploadPhoto(String path) {
        File file = new File(path);
        MyLog.i("wmm", "file path" + file.getAbsolutePath()    + "size   " + file.getName() + "   " + file.length());
        final UserInfo user = App.getInstance().getUser();
        MyLog.i("wmm", user.toString());

        Bitmap photo = BitmapFactory.decodeFile(file.getPath());
        iv_head_icon.setImageBitmap  (BitmapUtil.getRoundedCornerBitmap(photo));
        if (user != null) {
            Api.uploadavatar(App.getInstance(), user.username, user.password, path, new DefaultResponsehandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    MyLog.i("wmm", "uploadPhoto   onSuccess   " + response.toString());
                    Api.loadDrawable(user.head_icon + "?time=" + System.currentTimeMillis(), iv_head_icon, R.drawable.default_head_icon);
//                    Picasso.with(App.getInstance()).load(user.head_icon).skipMemoryCache().placeholder(R.drawable.default_head_icon).into(iv_head_icon);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    MyLog.i("wmm", "uploadPhoto   onFailure   " + responseString.toString());

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                    MyLog.i("wmm", "uploadPhoto   onSuccess   1" + response.toString());
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    super.onSuccess(statusCode, headers, responseString);
                    MyLog.i("wmm", "uploadPhoto   onSuccess   2" + responseString.toString());
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                    MyLog.i("wmm", "uploadPhoto   onFailure  1   " + statusCode + "     " + throwable.getMessage());
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);
                    MyLog.i("wmm", "uploadPhoto   onFailure  2   " + errorResponse.toString());
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    MyLog.i("wmm", "upload onFinish");
                }

                @Override
                public void onStart() {
                    super.onStart();
                    MyLog.i("wmm", "upload onStart");
                }
            });
        }
    }
   public String getRealPathFromURI(Uri contentUri) {
        String res = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        if(cursor != null){
            cursor.close();
        }
        return res;
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989542&siteId=291194637