调用系统相机,相册

compile 'com.github.zhaolei9527:BottomMenu:v1.0.1'
compile 'com.lovedise:permissiongen:0.0.6'

----------------------MainActivity---------------------

package com.example.earl;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import sakura.bottommenulibrary.bottompopfragmentmenu.BottomMenuFragment;
import sakura.bottommenulibrary.bottompopfragmentmenu.MenuItem;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final static int PHOTO_CHOOSE = 0;
    private final static int TAKE_A_PICTURE = 1;
    private ImageView img;
    //保存像片的路径
    private String saveImagePath = "";
    private Uri imageUriFromCamera;

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


        img = findViewById(R.id.img);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(this);
        //拼接路径-在sd卡的根目录下
        saveImagePath = Environment.getExternalStorageDirectory().getPath() + "/myimg.jpg";

    }

    @Override
    public void onClick(View v) {

        new BottomMenuFragment(MainActivity.this)
                .addMenuItems(new MenuItem("从相册选择"))
                .addMenuItems(new MenuItem("拍照"))
                .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() {
                    @Override
                    public void onItemClick(TextView menu_item, int position) {
                        Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show();

                        switch (position) {
                            case PHOTO_CHOOSE:
                                invokepics();
                                Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show();
                                break;
                            case TAKE_A_PICTURE:
                                invokecarema();
                                break;
                        }
                    }
                })
                .show();
    }

    /**
     * 调用系统相机
     */
    private void invokecarema() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //给像机一个指令  进行照片的保存         MediaStore.EXTRA_OUTPUT:输出
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(saveImagePath)));
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        imageUriFromCamera = FileProvider.getUriForFile(MainActivity.this,
                "com.xuezj.fileproviderdemo.fileprovider", new File(saveImagePath));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUriFromCamera);

        startActivityForResult(intent, 800);
    }

    /**
     * 调用系统相册
     */
    public void invokepics() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");//设置类型
        startActivityForResult(intent, 2000);
    }

    /**
     * 回调方法
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 800) {
            //加载显示 sd卡拍摄的照片
            img.setImageURI(Uri.fromFile(new File(saveImagePath)));


        } else if (requestCode == 9999) {//调用裁剪组件的请求码

            //得到裁剪后的照片进行显示
            Bitmap bitmap = data.getParcelableExtra("data");
            img.setImageBitmap(bitmap);

        }
        if (requestCode == 2000) {
            //从相册中得到照片-得到的是一个路径
            Uri data2 = data.getData();
            img.setImageURI(data2);

        }

        super.onActivityResult(requestCode, resultCode, data);
    }



}

--------------------activity_main.xml----------------------

<ImageView
    android:layout_marginTop="200dp"
    android:id="@+id/img"
    android:layout_gravity="center"
    android:layout_width="120dp"
    android:layout_height="120dp" />

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="弹框"
    />
----------------- res-->xml-->file_paths.xml -----------------

--------------------file_paths.xml--------------------

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <!--填写你要所要申请访问的目录地址,name最好是你的目录名,path是你要申请的目录-->
    <external-path name="camera_photos" path="."  />
    <external-path name="cache" path="Android/data/com.xuezj.fileproviderdemo/cache" />
    <external-path name="images" path="Pictures/" />
    <external-path name="dcim" path="DCIM/" />
</paths>
-----------------------AndroidManifest.xml----------------------
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.xuezj.fileproviderdemo.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40116418/article/details/79964489
上一篇Retrofit和RxJava的网络请求封装
下一篇使用git上传项目到码云服务器
想对作者说点什么? 我来说一句

android圆形头像

2016年05月03日 10.53MB 下载

没有更多推荐了,返回首页

关闭
关闭