调用相册相机

来源:互联网 发布:手机锁帧软件 编辑:程序博客网 时间:2024/05/17 03:43
public class MainActivity extends AppCompatActivity {    private TextView gerenname;    private TextView gerentoken;    private RelativeLayout huantouxiang;    private ImageView gerenTouXiang;    private LinearLayout anniu;    private Button xuanze;    private Button paizhao;    private Button quxiao;    private MyPresten myPresten;    String path="";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gerenname = (TextView) findViewById(R.id.gerenname);        gerentoken = (TextView) findViewById(R.id.gerentoken);        huantouxiang = (RelativeLayout) findViewById(R.id.huantouxiang);        gerenTouXiang = (ImageView) findViewById(R.id.gerenTouXiang);        anniu = (LinearLayout) findViewById(R.id.anniu);        xuanze = (Button) findViewById(R.id.xuanze);        paizhao = (Button) findViewById(R.id.paizhao);        quxiao = (Button) findViewById(R.id.quxiao);        myPresten = new MyPresten();        getShuJu();        huantouxiang.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                anniu.setVisibility(View.VISIBLE);                xuanze.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        Toast.makeText(MainActivity.this,"选择",Toast.LENGTH_SHORT).show();                        //打开相册                        Intent intent2 = new Intent(Intent.ACTION_PICK);                        intent2.setType("image/*");                        startActivityForResult(intent2, 200);                    }                });                paizhao.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        Toast.makeText(MainActivity.this,"拍照",Toast.LENGTH_SHORT).show();                        //启动相机                        Intent intent = new Intent();                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);                        // 指定调用相机拍照后照片的储存路径                        File file1 = new File("/storage/emulated/0/DCIM/Camera/" + getPhotoFileName());                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file1));                        path=file1.getPath();//记录当前路径                        startActivityForResult(intent,100);                    }                });                quxiao.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        anniu.setVisibility(View.INVISIBLE);                    }                });            }        });    }    private void getShuJu() {        myPresten.getContent(APIurl.geren, new MyJieKou() {            @Override            public void onChengGong(final String json) {                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        Gson gson = new Gson();                        MygerenBean mygerenBean = gson.fromJson(json, MygerenBean.class);                        if(mygerenBean.getCode().equals("0")){                            gerenname.setText(mygerenBean.getData().getUsername());                            gerentoken.setText(mygerenBean.getData().getToken());                            ImageLoader.getInstance().displayImage(mygerenBean.getData().getIcon(),gerenTouXiang, ImageLoaderUtil.showImage());                        }else{                            Toast.makeText(MainActivity.this,mygerenBean.getMsg(),Toast.LENGTH_SHORT).show();                        }                    }                });            }            @Override            public void onShiBai(final String ss) {                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        Toast.makeText(MainActivity.this,ss,Toast.LENGTH_SHORT).show();                    }                });            }        });    }    @Override    protected void onStart() {        super.onStart();    }    @Override    protected void onResume() {        super.onResume();    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if(requestCode==100&resultCode==RESULT_OK){//            拍照做了存储操作后,不允许做直接回执的操作,如果一定要做回执,可以通过裁剪的方式//            Parcelable pp = data.getParcelableExtra("data");//            gerenTouXiang.setImageBitmap((Bitmap) pp);            File file = new File(path);//拿到存储时所记录的路径            Log.i("jiab","===="+file.getPath());            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1001);            }else {                //允许...上传文件                postFile(file);                anniu.setVisibility(View.INVISIBLE);            }        }else if(requestCode==200&resultCode==RESULT_OK){            Uri data2 = data.getData();            //uri路径转成绝对路径            String[] proj = { MediaStore.Images.Media.DATA };            Cursor actualimagecursor = managedQuery(data2,proj,null,null,null);            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);            actualimagecursor.moveToFirst();            String img_path = actualimagecursor.getString(actual_image_column_index);            File file = new File(img_path);//            Uri uri = Uri.fromFile(file);  //file类型转成uri            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1001);            }else {                //允许...上传文件                postFile(file);                anniu.setVisibility(View.INVISIBLE);            }        }    }        //2.指定文件的类型 image/jpg image/png video/mp4 ...mimeType//text/plain; charset=utf-8    //text/x-markdown;charset=utf-8    private void postFile(File file) {        OkHttpClient okHttpClient = new OkHttpClient();        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);        //3.指定要上传的文件对象//        File file = new File(Environment.getExternalStorageDirectory(),"fruit8.jpg");//        Log.i("jiaaa","===="+file.getPath());               //参数名,,,参数,,,参数类型,,,路径        builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));        MultipartBody requestBody = builder.build();        Request request = new Request.Builder()                .post(requestBody)//请求体                .url(APIurl.chuan(file.getPath()))//指定路径                .build();//启动        Call call = okHttpClient.newCall(request);        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                e.printStackTrace();            }            @Override            public void onResponse(Call call, final Response response) throws IOException {                final String string = response.body().string();                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();                        getShuJu();                    }                });            }        });    }    public void tui(View view) {        finish();    }    private String getPhotoFileName() {        Date date = new Date(System.currentTimeMillis());        SimpleDateFormat dateFormat = new SimpleDateFormat(                "'IMG'_yyyyMMdd_HHmmss");        return dateFormat.format(date) + ".jpg";    }}
//布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="one.bw.com.rikao1208.view.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="300dp"        android:orientation="vertical">        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text=""                android:textSize="30px"                android:layout_centerVertical="true"                android:id="@+id/fan1"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="个人信息"                android:textSize="20px"                android:layout_centerInParent="true"/>        </RelativeLayout>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="头像"                android:textSize="20px"                android:layout_centerVertical="true"                android:layout_marginLeft="15px"                />            <RelativeLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentRight="true"                android:id="@+id/huantouxiang"                >                <ImageView                    android:layout_width="80dp"                    android:layout_height="80dp"                    android:id="@+id/gerenTouXiang"                    android:layout_alignParentRight="true"                    android:layout_marginRight="30dp"                    android:layout_centerVertical="true"/>                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerVertical="true"                    android:layout_alignParentRight="true"                    android:text=">"                    android:textSize="30dp"                    android:layout_marginRight="5dp"                    />            </RelativeLayout>        </RelativeLayout>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="用户名"                android:textSize="20px"                android:layout_centerVertical="true"                android:layout_marginLeft="15px"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/gerenname"                android:layout_alignParentRight="true"                android:layout_marginRight="20px"                android:layout_centerVertical="true"/>        </RelativeLayout>        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="昵称"                android:textSize="20px"                android:layout_centerVertical="true"                android:layout_marginLeft="15px"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/gerentoken"                android:layout_alignParentRight="true"                android:layout_marginRight="20px"                android:layout_centerVertical="true"/>        </RelativeLayout>        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="退出"            android:onClick="tui"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:visibility="invisible"        android:id="@+id/anniu"        android:orientation="vertical">        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="从相册选择"            android:id="@+id/xuanze"            android:background="@drawable/kuang"            />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="拍照"            android:id="@+id/paizhao"            android:background="@drawable/kuang"            />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="取消"            android:id="@+id/quxiao"            android:background="@drawable/kuang"            />    </LinearLayout></RelativeLayout>
原创粉丝点击