Android学习笔记:获取相册和摄像机图片在APP显示Demo

一直在使用csdn网站学习,虽然很早就注册了账号,但是没有在上面发表过一篇blog,我现在发表的算是第一篇blog吧,以此作为纪念.
最近在学习Android开发,都是一个人在摸索,这篇blog主题是怎样在app中调用相册和摄像机图片,步骤如下:
一、在Android Studio内新建一个项目,取名TestPic,
二、配置Manifest.xml权限,如下

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

三、在activity_main.xml布局文件内放置两个按钮和一个图像组件

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btLoadPicture"
        android:text="LoadPicture"
        android:onClick="onLoadPictureClick"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btTakePicture"
        android:text="TakePicture"
        android:onClick="onTakePictureClick"/>

LinearLayout>


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ivImage"/>

四、编写Main_Activity界面代码,如下:

package com.test.administrator.testpic;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_PICK_IMAGE = 1;
    private static final int REQUEST_TAKE_PICTURE = 0;
    ImageView imageView;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.ivImage);
    }

    public void onLoadPictureClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUEST_PICK_IMAGE);
    }

    public void onTakePictureClick(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        file = new File(file, "picture.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        if (isIntentAvailable(this, intent)) {
            startActivityForResult(intent, REQUEST_TAKE_PICTURE);
        } else {
            Toast.makeText(this, "NoCameraAppsFound", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean isIntentAvailable(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = null;
        if(resultCode == RESULT_OK){
            switch (requestCode){
                case REQUEST_PICK_IMAGE:
                    Uri uri = data.getData();
                    String[] columns = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(uri, columns, null, null, null);
                    if(cursor.moveToFirst()){
                        String imagePath = cursor.getString(0);
                        cursor.close();
                         bitmap = BitmapFactory.decodeFile(imagePath);

                    }
                    break;
                case REQUEST_TAKE_PICTURE:
                    // Bitmap picture = (Bitmap) intent.getExtras().get("data");
                    bitmap = BitmapFactory.decodeFile(file.getPath());


                    break;
            }
        }
        imageView.setImageBitmap(bitmap);
    }
}

五、测试,如果是模拟机,必须在模拟机内放置图片
注意的地方:我在测试代码的时候发现一个问题,在使用sdk版本为api23以上时,运行时会出现 permission denied错误,刚开始一直不知道原因在什么地方,出错的语句在getContentResolver().query这个方法上,解决的方法是,采用api版本不能23以上,我用api22测试成功,如果确实采用api23以上,也就是Android6.0,我们注意到6.0后Google新增动态权限管理,那就需要对权限加以判断才行。

你可能感兴趣的:(Android)