Android开发开启照相机并保存本地显示高清照片

释放双眼,带上耳机,听听看~!

1.activity_main.xml先对界面进行布局:

先上图:

<LinearLayout 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"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/ImageView01"
android:scaleType="centerCrop"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="https://blog.csdn.net/hdhhd/article/details/100074351/@mipmap/tx"
app:riv_oval="true"
app:riv_corner_radius="100dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ImageView01"
android:layout_alignParentBottom="true"
android:layout_marginBottom="83dp"
android:text="拍照" />
</LinearLayout>

圆形头像框的RoundedImageView的使用:https://blog.csdn.net/hdhhd/article/details/100073735

MainActivity:
package youli.com.example.administrator.my_camera_demo;
import android.app.Activity;
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.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.makeramen.roundedimageview.RoundedImageView;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private RoundedImageView image;
private File currentImageFile = null;
private Bitmap img;
private String path;
private File sdCardDir = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn1 = findViewById(R.id.button1); //拍照
image = findViewById(R.id.ImageView01); //显示照片
//        img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
//
//        image.setImageBitmap(img);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File dir = new File(sdCardDir + "/LLWDCZ/lxd_ck/");
dir.mkdirs();
currentImageFile = new File(dir, "ck.jpg");
path = currentImageFile.getPath();
Log.e("QWE", path + "显示路径");
if (!currentImageFile.exists()) {
try {
currentImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));
startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
img = getBitemapFromFile(path);
image.setImageBitmap(img);
}
//根据路径来寻找图片并把图片转为Bitmap形式(重要)
@SuppressWarnings("deprecation")
public static Bitmap getBitemapFromFile(String fileName) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
opt.inSampleSize = 2;
try {
return BitmapFactory.decodeFile(fileName, opt);
} catch (Exception ex) {
return null;
}
}
}

最后别忘了权限:在AndroidManifest文件中加入:

 <!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name = "android.hardware.camera" />
<uses-feature android:name = "android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

OK,欢迎留言

人已赞赏
Android文章

Android 底部导航栏 开源JPTabBar

2020-2-29 10:27:10

Android文章

android开发圆形图片开源框架roundedimageview的使用

2020-2-29 12:27:07

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索