您的位置:首页 > 移动开发 > Android开发

Android 7.0拍照并保存到手机的指定目录

2018-05-16 15:57 148 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vampire_1122/article/details/80337890

本文参考第一行代码Android第2版。实现拍照并保存到指定目录。

1、创建项目Camera,修改activity_main.xml中的代码如下:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--点击拍照-->
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/take_photo"/>

<!--显示照片-->
<ImageView
android:id="@+id/show_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

</LinearLayout>

Button控件用于点击拍照,ImageView控件用于显示拍摄的照片。

2、接下来修改MainActivity 中的代码。

[code]public class MainActivity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;
private ImageView picture;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takePhoto = (Button) findViewById(R.id.take_photo);
picture = (ImageView) findViewById(R.id.show_photo);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//创建File对象,用于保存拍摄的照片
File outputImage = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/"+System.currentTimeMillis() + ".jpg");
if (!outputImage.getParentFile().exists()){
outputImage.getParentFile().mkdirs();
}
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.camera.fileprovider", outputImage);
} else {
imageUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}
});
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch (requestCode){
case TAKE_PHOTO:
if (resultCode == RESULT_OK){
try {//显示照片
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}

这里将拍摄的保存路径设置为:“/DCIM/Camera/”。

3、在AndroidManifest.xml中对内容提供器进行注册。

[code]        <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.camera.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

4、在res目录下新建目录xml,并创建文件file_paths.xml。

[code]<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="" />
</paths>

5、在AndroidManifest.xml中进行SD卡和相机访问权限声明,并调试运行。

[code]    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>

 本人调试机型 小米4c,截图就不附上了。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: