Imageview 拒绝显示图像
Imageview refuses to display image
我尝试了很多不同的方法来使图片显示在我的 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() return 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 不再为空。图像仍然没有出现。不过,我拍摄的所有照片都在我 phone 的相册中,所以很省钱。
有问题的 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);
如果你需要用相机拍照并在图像视图中显示它(这是我的理解)我认为这可以帮助你
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;
}
保存和恢复activity
状态的方法
@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);
}
}
activity 结果(您的 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();
}
}
}
希望对您有所帮助。
试试这个对我有用
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);
我尝试了很多不同的方法来使图片显示在我的 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() return 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 不再为空。图像仍然没有出现。不过,我拍摄的所有照片都在我 phone 的相册中,所以很省钱。 有问题的 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);
如果你需要用相机拍照并在图像视图中显示它(这是我的理解)我认为这可以帮助你
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;
}
保存和恢复activity
状态的方法@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);
}
}
activity 结果(您的 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();
}
}
}
希望对您有所帮助。
试试这个对我有用
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);