Android studio可以实现拍照但是无法保存到imageview Uri无法实现
时间: 2023-06-09 12:07:36 浏览: 154
这个问题可能是因为您没有正确地处理文件路径和权限。您需要确保您的应用程序具有适当的权限来读取和写入文件。您还需要处理好文件路径,确保它指向正确的位置。您可以尝试在保存图像之前打印文件路径来进行调试。另外,您可以尝试使用以下代码将图像添加到 ImageView 中:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.parse("file:///sdcard/image.jpg"));
请注意,您需要将“/sdcard/image.jpg”替换为您保存图像的实际路径。
相关问题
Android studio调用相机拍照并显示到ImageView中
好的,这是一个比较常见的需求,下面是实现的步骤:
1. 添加权限
在 AndroidManifest.xml 文件中添加相机和文件读写权限:
```xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
2. 添加布局
在布局文件中添加一个 ImageView 和一个 Button:
```xml
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" />
```
3. 调用相机拍照
在按钮的点击事件中调用相机拍照,并将照片保存到本地:
```java
private static final int REQUEST_CODE_CAMERA = 1;
private String mImagePath;
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建拍照的 Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 创建保存图片的文件
File file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg");
mImagePath = file.getAbsolutePath();
// 设置拍照后保存照片的路径
Uri uri = FileProvider.getUriForFile(MainActivity.this,
getPackageName() + ".fileprovider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
// 调用相机拍照
startActivityForResult(intent, REQUEST_CODE_CAMERA);
}
});
```
在拍照后,会将照片保存到指定的路径中。
4. 显示照片
在 onActivityResult 方法中获取拍照后的照片,并显示到 ImageView 中:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
// 显示照片
Bitmap bitmap = BitmapFactory.decodeFile(mImagePath);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
}
}
```
这样就可以实现调用相机拍照并显示到 ImageView 中了。
Android Studio 拍照后imageview显示图片
您好,关于Android Studio拍照后ImageView显示图片的问题,可以通过以下步骤来实现:
1. 在AndroidManifest.xml文件中添加相机和写入外部存储的权限:
```
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
```
2. 在布局文件中添加一个ImageView来显示图片:
```
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/image_desc"/>
```
3. 在Activity中添加一个按钮来触发拍照功能,同时在onCreate方法中获取ImageView的引用:
```
private ImageView imageView;
private static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
});
}
```
4. 在Activity中添加拍照的处理逻辑,使用Intent来调用系统自带的相机应用,拍完照后将照片保存到指定路径,并显示在ImageView上:
```
private File photoFile = null;
private String currentPhotoPath = null;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Set the image to ImageView
setPic();
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void setPic() {
// Get the dimensions of the View
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
imageView.setImageBitmap(bitmap);
}
```
以上就是通过Android Studio实现拍照后ImageView显示图片的步骤,如果还有问题可以继续提问哦。
阅读全文