Android 相册及拍照 相关问题

获取相册:
1.调起系统相册

Intent intent;
if ( currentVersion < Build.VERSION_CODES.N ) {
	intent = new Intent( Intent.ACTION_PICK, null );
} else {
	intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
}
intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, “image/*” );
startActivityForResult( intent, Constant.IMAGE_ALBUM );

2.获取相册图片,可直接在onActivityResult()中直接获取,也可以修剪图片

Intent intent = new Intent();
intent.setAction( “com.android.camera.action.CROP” );
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ) {
	intent.addFlags( Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
	intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
	intent.putExtra( “noFaceDetection”, true );
	outputImagepath = new File(
	Environment.getExternalStorageDirectory() + “/life/”,
	String.valueOf( System.currentTimeMillis() ) + “.jpg” );
	intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( outputImagepath ) );
	intent.putExtra( “outputFormat”, Bitmap.CompressFormat.JPEG.toString() );
}
// mUri是已经选择的图片Uri
intent.setDataAndType( data.getData(), “image/*” );
intent.putExtra( “crop”, “true” );
// 裁剪框比例
intent.putExtra( “aspectX”, 2 );
intent.putExtra( “aspectY”, 1 );
// 输出图片大小
intent.putExtra( “outputX”, 400 );
intent.putExtra( “outputY”, 250 );
intent.putExtra( “return-data”, true );
startActivityForResult( intent, Constant.IMAGE_RESULT );

拍照:

Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
File appDir = new File( Environment.getExternalStorageDirectory() + “/life” );
if ( !appDir.exists() ) {
	appDir.mkdir();
}
outputImagepath = new File(
Environment.getExternalStorageDirectory() + “/life/”,
String.valueOf( System.currentTimeMillis() ) + “.jpg” );
if ( currentVersion < Build.VERSION_CODES.N ) {
	mUri = Uri.fromFile( outputImagepath );
	intent.putExtra( MediaStore.EXTRA_OUTPUT, mUri );
} else {
	// ContentValues contentValues = new ContentValues( 1 );
	// contentValues.put( MediaStore.Images.Media.DATA, outputImagepath.getAbsolutePath() );
	// Uri uri = getApplication().getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues );
	// intent.putExtra( MediaStore.EXTRA_OUTPUT, uri );
	mUri = FileProvider.getUriForFile( ActivityCreateActivity.this, “com.changjv.app.fileProvider”, outputImagepath );
	intent.putExtra( MediaStore.EXTRA_OUTPUT, mUri );
}
// intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
intent.putExtra( “return-data”, true );
startActivityForResult( intent, Constant.IMAGE_CAPTURE );

2.可直接在onActivityResult()中直接获取,也可以修剪图片

	String sdStatus = Environment.getExternalStorageState();
            if ( !sdStatus.equals( Environment.MEDIA_MOUNTED ) ) {
                // 检测sd是否可用
                Log.i( "TestFile", "SD card is not avaiable/writeable right now." );
                return;
            }
            ContentResolver cr = this.getContentResolver();
            Bitmap bmp_selectedPhoto = null;
            try {
                bmp_selectedPhoto = BitmapFactory.decodeStream( cr.openInputStream( mUri ) );
            } catch ( FileNotFoundException e ) {
                e.printStackTrace();
            }
            if ( bmp_selectedPhoto == null ) {
                return;
            }
            outputUri = Uri.parse( MediaStore.Images.Media.insertImage( getContentResolver(), bmp_selectedPhoto, null, null ) );
            intent = new Intent();
            intent.setAction( "com.android.camera.action.CROP" );
            intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
            intent.addFlags( Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
            // mUri是已经选择的图片Uri
            intent.setDataAndType( outputUri, "image/*" );
            intent.putExtra( "crop", "true" );
            // 裁剪框比例
            intent.putExtra( "aspectX", 2 );
            intent.putExtra( "aspectY", 1 );
            // 输出图片大小
            intent.putExtra( "outputX", 400 );
            intent.putExtra( "outputY", 250 );
	//intent.putExtra( "scale", true );
            intent.putExtra( "return-data", true );
            outputImagepath = new File(
                    Environment.getExternalStorageDirectory() + "/life/",
                    String.valueOf( System.currentTimeMillis() ) + ".jpg" );
            intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( outputImagepath ) );
            intent.putExtra( "outputFormat", Bitmap.CompressFormat.JPEG.toString() );
            intent.putExtra( "noFaceDetection", true );

            startActivityForResult( intent, Constant.IMAGE_RESULT );

3.拿到裁剪的路径:

Bitmap upbitmap = null;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ) {
	ContentResolver Resolver = this.getContentResolver();
	try {
	upbitmap = BitmapFactory.decodeStream( Resolver.openInputStream( Uri.fromFile( outputImagepath ) ) );
	} catch ( FileNotFoundException e ) {
	e.printStackTrace();
	}
	if ( upbitmap == null ) {
	return;
	}
} else {
	upbitmap = data.getParcelableExtra( “data” );
	if ( upbitmap == null ) {
		ToastUtil.toast( “没有获取到图片连接” );
		return;
	}
	// 显示剪切的图像
}

1.获取拍照图片时,data返回为null,这个是一个坑。
2.API>=23,权限问题,又是一个坑,API>=26,权限问题相对于API23有所变化;这里不详细描述,另外保存。
3.存储图片也是一个坑:
FileProvider

在AndroidMinfest中配置:

	<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
	</provider>

在xml文件中设置:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="my_images"
        path="images/"/>
    <external-path
        name="external_files"
        path="."/>
    <external-path
        name="images"
        path="meta"/>
    <external-cache-path name="mycache" path="./" />
</paths>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值