Androidで写真を撮ったり、アルバムから写真を選んだり

4651 ワード

Activity:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;


public class XJ_Activity extends AppCompatActivity implements View.OnClickListener {
    private Button tt;
    private ImageView imgxx;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xj);
        initView();
    }

    private void initView() {
        tt = (Button) findViewById(R.id.tt);
        imgxx = (ImageView) findViewById(R.id.imgxx);

        tt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tt:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        String sj[] = {"  ", "  "};
                        builder.setItems(sj, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:
                                        xj();
                                        break;
                                    case 1:
                                        xc();
                                        break;

                                }

                            }
                        });
                        //  
                        builder.setNegativeButton("  ", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                            }
                        });
                        builder.show();

                break;
        }
    }
    private void xc() {

        Intent intentxc = new Intent();
        //       
        intentxc.setType("image/*");
        intentxc.setAction(Intent.ACTION_PICK);
        startActivityForResult(intentxc, 100);

    }
    private void xj() {
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"lyq.jpg")));

        startActivityForResult(intent,200);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==100&&resultCode==RESULT_OK){
            cj(data.getData());
        }
        if (requestCode==200&&resultCode==RESULT_OK){
            File tempFile = new File(
                    Environment.getExternalStorageDirectory(),
                    "lyq.jpg");


         cj(Uri.fromFile(tempFile));
        }

        if (requestCode==300&&resultCode==RESULT_OK){

            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                imgxx.setImageBitmap(photo);

            }
        }
    }
    public void cj(Uri uri){

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");

        //     
        intent.putExtra("crop", "true");

        // aspectX , aspectY :     
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX , outputY :       
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, 300);
    }
}


アクセス権: