Android拍照或者从相册选取图片加载在imageview上,代码怎么写
时间: 2023-09-03 09:26:58 浏览: 158
android通过访问相册获取图片并展示在ImageView中
### 回答1:
你可以使用以下代码实现:
// 拍照
private static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
// 从相册选取图片
private static final int REQUEST_IMAGE_GET = 2;
private void dispatchGetPictureIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
try {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
以上代码中,dispatchTakePictureIntent() 方法用于启动相机拍照,dispatchGetPictureIntent() 方法用于从相册选取图片。在 onActivityResult() 方法中,根据 requestCode 和 resultCode 判断是哪种操作,然后获取图片并设置到 imageView 上。
### 回答2:
在Android中,你可以通过以下方式拍照或从相册中选取图片并将其加载在ImageView上。
首先,在你的布局文件中添加一个ImageView元素,用于显示拍照或选取的照片:
```xml
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
然后,在你的Activity或Fragment中,通过以下代码实现拍照或选取照片的功能:
```java
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA = 1;
private static final int REQUEST_GALLERY = 2;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
}
public void takePhoto(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}
} else {
openCamera();
}
}
public void openGallery(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_GALLERY);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_GALLERY);
}
} else {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_GALLERY);
}
}
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_GALLERY) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openGallery(null);
} else {
Toast.makeText(this, "Storage permission denied", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
} else if (requestCode == REQUEST_GALLERY) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
}
```
以上代码中,我们首先检查相机和存储权限。如果权限已经授予,我们就可以打开相机或者打开相册。在onActivityResult()方法中,我们从返回的数据获取照片,并将其设置在ImageView上。
为了正常运行,请确保在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
还需要添加所需的Activity:
```xml
<activity android:name=".MainActivity">
// ...
</activity>
```
以上就是通过Android拍照或从相册选取图片并加载在ImageView上的代码实现。
### 回答3:
在Android平台上,我们可以通过一些简单的代码实现拍照或从相册选取图片,并将其加载到ImageView上。
1. 拍照功能:
首先,我们需要在manifest文件中添加相机权限。在AndroidManifest.xml文件中添加以下行:
```
<uses-permission android:name="android.permission.CAMERA" />
```
然后,在点击按钮或其他触发事件时,使用以下代码来实现拍照功能:
```java
private static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(imageBitmap);
}
}
```
2. 从相册选取图片:
同样,我们需要在manifest文件中添加读取相册权限。在AndroidManifest.xml文件中添加以下行:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
接下来,在点击按钮或其他触发事件时,可以使用以下代码实现从相册选取图片功能:
```java
private static final int PICK_IMAGE_REQUEST = 2;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上是拍照和从相册选取图片加载在ImageView上的简单示例代码。不同的项目和需求可能会有所差异,您可以根据您的具体情况进行调整和扩展。
阅读全文