zuo.biao.library.ui.SelectPictureActivity.java Source code

Java tutorial

Introduction

Here is the source code for zuo.biao.library.ui.SelectPictureActivity.java

Source

/*Copyright 2015 TommyLemon(https://github.com/TommyLemon)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package zuo.biao.library.ui;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import java.io.File;

import zuo.biao.library.R;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseApplication;
import zuo.biao.library.util.CommonUtil;
import zuo.biao.library.util.DataKeeper;

/**?Activity,
 * @author Lemon
 * @use
 * <br> toActivitystartActivityForResult (SelectPictureActivity.createIntent(...), requestCode);
 * <br> ?onActivityResult
 * <br> data.getStringExtra(SelectPictureActivity.RESULT_PICTURE_PATH); ?
 */
public class SelectPictureActivity extends BaseActivity implements OnClickListener {
    //   private static final String TAG = "SelectPictureActivity";

    //?<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, SelectPictureActivity.class);
    }

    //?<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_picture_activity);

        //<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //>>>>>>>>>>

    }

    //UI(?UI????????)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initView() {//

    }

    //UI(?UI????????)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data?(???????)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private String picturePath = "";

    @Override
    public void initData() {//

    }

    private File cameraFile;

    /**
     * ?
     */
    public void selectPicFromCamera() {
        if (!CommonUtil.isExitsSdcard()) {
            showShortToast("SD????");
            return;
        }

        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // ??
        cameraFile = new File(DataKeeper.imagePath, "photo" + System.currentTimeMillis() + ".jpg");
        cameraFile.getParentFile().mkdirs();

        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context,
                    BaseApplication.getInstance().getApplicationInfo().packageName + ".fileProvider", cameraFile);
        } else {
            uri = Uri.fromFile(cameraFile);
        }

        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        toActivity(intent, REQUEST_CODE_CAMERA);
    }

    /**
     * ?
     */
    public void selectPicFromLocal() {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
        } else {
            intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        toActivity(intent, REQUEST_CODE_LOCAL);
    }

    public static final String RESULT_PICTURE_PATH = "RESULT_PICTURE_PATH";

    /**?uri??
     * @param selectedImage
     */
    private void sendPicByUri(Uri selectedImage) {
        Cursor cursor = getContentResolver().query(selectedImage, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex("_data");
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            cursor = null;

            if (picturePath == null || picturePath.equals("null")) {
                Toast toast = Toast.makeText(this, "?", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                return;
            }
        } else {
            File file = new File(selectedImage.getPath());
            if (!file.exists()) {
                Toast toast = Toast.makeText(this, "?", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                return;

            }
            picturePath = file.getAbsolutePath();
        }
        setResult(RESULT_OK, new Intent().putExtra(RESULT_PICTURE_PATH, picturePath));
    }

    //Data?(???????)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event(????)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {//

        findViewById(R.id.llSelectPictureBg).setOnClickListener(this);

        toActivity(
                new Intent(context, BottomMenuWindow.class).putExtra(BottomMenuWindow.INTENT_TITLE, "")
                        .putExtra(BottomMenuWindow.INTENT_ITEMS, new String[] { "?", "" }),
                REQUEST_TO_BOTTOM_MENU, false);
    }

    //?<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.llSelectPictureBg) {
            finish();
        }
    }

    //?<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    public static final int REQUEST_TO_BOTTOM_MENU = 10;
    public static final int REQUEST_CODE_CAMERA = 18;
    public static final int REQUEST_CODE_LOCAL = 19;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU:
                if (data != null) {
                    switch (data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1)) {
                    case 0:
                        selectPicFromCamera();//
                        return;
                    case 1:
                        selectPicFromLocal();//
                        return;
                    default:
                        break;
                    }
                }
                break;
            case REQUEST_CODE_CAMERA: //??
                if (cameraFile != null && cameraFile.exists()) {
                    picturePath = cameraFile.getAbsolutePath();
                    setResult(RESULT_OK, new Intent().putExtra(RESULT_PICTURE_PATH, picturePath));
                }
            case REQUEST_CODE_LOCAL: //??
                if (data != null) {
                    Uri selectedImage = data.getData();
                    if (selectedImage != null) {
                        sendPicByUri(selectedImage);
                    }
                }
                break;
            default:
                break;
            }
        }

        finish();
    }

    @Override
    public void finish() {
        exitAnim = enterAnim = R.anim.null_anim;
        super.finish();
    }

    //?>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //?>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event(????)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //,?<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    //,?>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}