Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

获取手机相机和相册可以传多张图片

$
0
0

最近开发的项目中需要手机相册和相机功能我就在各种博客中找到了一些资料在综合我的就可以实现多张照片的展示

自我感觉要比其它的博客写的详细

直接上图了大家:

                                                                            

直接上代码:

千万不要忘记在清单文件中加上权限:

    <!--请求相机资源权限,android:required="false"这个属性表示允许不带相机的设备也可安装这个App,设为true就是不允许-->
    <uses-permission android:name="android.permission.CAMERA" android:required="false"/>
    <!--使用相机资源权限-->
    <uses-feature android:name="android.hardware.camera" />
    <!--读写SD卡权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/takephoto_photo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/takephoto_text"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="相册"
            android:textSize="20sp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/line1"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/takephoto_picture"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/publish_phtot_rim" />

        <ImageView
            android:id="@+id/takephoto_picture2"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/publish_phtot_rim" />

        <ImageView
            android:id="@+id/takephoto_picture3"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/publish_phtot_rim" />

        <ImageView
            android:id="@+id/takephoto_picture4"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/publish_phtot_rim" />

        <ImageView
            android:id="@+id/takephoto_picture5"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:background="@drawable/publish_phtot_rim" />


    </LinearLayout>

    <TextView
        android:id="@+id/takephtot_jump"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:text="上传照片"
        android:textSize="20sp" />

</RelativeLayout>

Activity中的代码

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import utils.ImageTools;

/**
 * Created by big_cow on 2016/11/29.
 */

public class TakePhotoActivity extends Activity {

    private static final int TAKE_PICTURE = 0; // 相机
    private static final int CHOOSE_PICTURE = 1; // 图片
    private static final int SCALE = 5;//照片缩小比例
    private ImageView takePhoto_picture, takePhotot_photo, takePhoto_picture2,
            takePhoto_picture3, takePhoto_picture4, takePhoto_picture5;
    private TextView takePhotot_text;
    private TextView takePhoto_jump;
    private ImageView[] imageViews;
    // 存放图片路径
    private ArrayList<String> mList = new ArrayList<String>();
    private HashMap<ImageView, PictureType> map = new HashMap<ImageView, PictureType>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.takephoto_item);
        initView();
        initButton_photo();
        initButoon_jump();
    }

    private void initView() {

        takePhotot_photo = (ImageView) findViewById(R.id.takephoto_photo);// 相机
        takePhotot_text = (TextView) findViewById(R.id.takephoto_text);  // 相册
        takePhoto_picture = (ImageView) findViewById(R.id.takephoto_picture); // 图片
        takePhoto_picture2 = (ImageView) findViewById(R.id.takephoto_picture2);   // 图片
        takePhoto_picture3 = (ImageView) findViewById(R.id.takephoto_picture3); // 图片
        takePhoto_picture4 = (ImageView) findViewById(R.id.takephoto_picture4); // 图片
        takePhoto_picture5 = (ImageView) findViewById(R.id.takephoto_picture5); // 图片
        /*
        imageViews = new ImageView[]
                {takePhoto_picture, takePhoto_picture2, takePhoto_picture3, takePhoto_picture4, takePhoto_picture5};*/
        takePhoto_jump = (TextView) findViewById(R.id.takephtot_jump); //发表

    }

 

    // 相机相册点击事件
    private void initButton_photo() {
        takePhotot_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
                //指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(openCameraIntent, TAKE_PICTURE);
            }
        });
        takePhotot_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
            }
        });
    }

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

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case TAKE_PICTURE:
                    takePhoto();
                    break;

                case CHOOSE_PICTURE:
                    takePicture(data);
                    break;

                default:
                    break;
            }
        }
    }

    private void takePicture(Intent data) {
        ContentResolver resolver = getContentResolver();
        //照片的原始资源地址
        Uri originalUri = data.getData();
        try {
            //使用ContentProvider通过URI获取原始图片
            Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri);
            if (photo != null) {
                //为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
                Bitmap smallBitmap = ImageTools.zoomBitmap(photo, photo.getWidth() / SCALE,
                        photo.getHeight() / SCALE);
                //释放原始图片占用的内存,防止out of memory异常发生
                photo.recycle();
                switch (map.size()) {
                    case 0:
                        takePhoto_picture.setImageBitmap(smallBitmap);
                        map.put(takePhoto_picture, PictureType.PICTURE);
                        String s = originalUri.toString();
                        mList.add(s);
                        break;
                    case 1:
                        takePhoto_picture2.setImageBitmap(smallBitmap);
                        map.put(takePhoto_picture2, PictureType.PICTURE);
                        Uri picture_uri2 = data.getData();
                        // 把Uri转化成String类型
                        String s2 = originalUri.toString();
                        mList.add(s2);
                        break;
                    case 2:
                        takePhoto_picture3.setImageBitmap(smallBitmap);
                        map.put(takePhoto_picture3, PictureType.PICTURE);
                        Uri picture_uri3 = data.getData();
                        String s3 = originalUri.toString();
                        mList.add(s3);
                        break;
                    case 3:
                        takePhoto_picture4.setImageBitmap(smallBitmap);
                        map.put(takePhoto_picture4, PictureType.PICTURE);
                        Uri picture_uri4 = data.getData();
                        String s4 = originalUri.toString();
                        mList.add(s4);
                        break;
                    case 4:
                        takePhoto_picture5.setImageBitmap(smallBitmap);
                        map.put(takePhoto_picture5, PictureType.PICTURE);
                        Uri picture_uri5 = data.getData();
                        String s5 = originalUri.toString();
                        mList.add(s5);
                        break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    private void takePhoto() {
        //将保存在本地的图片取出并缩小后显示在界面上
        Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/image.jpg");
        Bitmap newBitmap = ImageTools.zoomBitmap(bitmap, bitmap.getWidth() / SCALE, bitmap.getHeight() / SCALE);
        //由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常
        bitmap.recycle();
        //将处理过的图片显示在界面上,
        switch (map.size()) {
            // 如果位置等于0的时候我们往HashMap中添加第一个图片,以后就以此类推
            case 0:
                takePhoto_picture.setImageBitmap(newBitmap);
                map.put(takePhoto_picture, PictureType.PHOTO);
                break;
            case 1:
                takePhoto_picture2.setImageBitmap(newBitmap);
                map.put(takePhoto_picture2, PictureType.PHOTO);
                break;
            case 2:
                takePhoto_picture3.setImageBitmap(newBitmap);
                map.put(takePhoto_picture3, PictureType.PHOTO);
                break;
            case 3:
                takePhoto_picture4.setImageBitmap(newBitmap);
                map.put(takePhoto_picture4, PictureType.PHOTO);
                break;
            case 4:
                takePhoto_picture5.setImageBitmap(newBitmap);
                map.put(takePhoto_picture5, PictureType.PHOTO);
                break;
        }

        // 并保存到本地

        ImageTools.savePhotoToSDCard(newBitmap, Environment.getExternalStorageDirectory().getAbsolutePath(),
                String.valueOf(System.currentTimeMillis()));
    }

    // 第一个枚举
    enum PictureType {
        PHOTO, PICTURE
    }

}




     

作者:Zzq_Big_Cow 发表于2016/12/6 20:00:59 原文链接
阅读:57 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>