我尝试过很多不同的方法让我的imageView中显示图片,但它只是拒绝这样做。我不知道发生了什么事。我的应用需要做的不仅仅是拍照并展示它。基本上所有这些代码都直接来自Android website。
private File createImageFile() throws IOException
{
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent()
{
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
// Log.d("myTag", "MADE IMAGE FILE");
} catch (IOException ex) {
// Log.d("myTag", "ERROR CREATING IMAGE FILE");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,"com.company.app.fileprovider.READ",photoFile);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// Log.d("myTag", "PUTEXTRA");
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
// Log.d("myTag", "ABOUTTOSTARTACTIVITY");
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_TAKE_PHOTO) {
setPic();
}
}
我在setPic()中唯一能注意到的是mImageView.getWidth()和getHeight()返回0和错误。我已经尝试过显示图像而没有缩放但仍然有相同的结果(或缺少)。我也尝试过getMeasuredWidth()并且它没有错误但仍然没有图像。
private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth(); //getMeasuredWidth()
int targetH = mImageView.getHeight(); //getMeasuredHeight()
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, 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(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
我的mCurrentPhotoPath不再为null。图像STILL未显示。我拍摄的所有照片都在我手机的图库中,所以它正在保存。 有问题的imageView的xml在这里:
ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_picture"
android:paddingLeft="250dp"
android:paddingBottom="250dp"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/home_button"
android:layout_marginRight="41dp"
android:layout_marginEnd="41dp" />
我的onCreate中有以下行:
mImageView = (ImageView) findViewById(R.id.main_picture);
答案 0 :(得分:1)
如果你需要用相机拍照并在图像视图中显示(这是我所理解的)我觉得这可以帮到你
Capture Image from Camera and Display in Activity
编辑:输入适合我的所有代码
调用意图
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
创建文件的方法
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
保存和恢复活动状态的方法
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//savedInstanceState.putString("filePath", mCurrentPhotoPath);
savedInstanceState.putString("filePath", mCurrentPhotoPath);
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
if(drawable!= null) {
Bitmap bitmap = drawable.getBitmap();
savedInstanceState.putParcelable("image", bitmap);
}
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null) {
mCurrentPhotoPath= savedInstanceState.getString("filePath");
Bitmap bitmap = savedInstanceState.getParcelable("image");
img.setImageBitmap(bitmap);
}
}
活动结果(你的setPic()方法)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
Bitmap mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
img.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
希望这可以帮到你。
答案 1 :(得分:0)
试试这对我有用
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 0;
File imageFile = new File("/sdcard/Cyclops/cyclops.jpg");
bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
imageView.setImageBitmap(bitmap);