【发布时间】:2016-07-23 16:18:40
【问题描述】:
我已经尝试了很多不同的方法来让图片显示在我的 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 不再为空。图像仍然不显示。我拍的所有照片都在我手机的画廊里,所以它正在保存。 有问题的 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);
【问题讨论】:
-
显示任何错误?在这种情况下发布你的 logcat
-
验证您是否在照片路径中收到任何内容?
-
(一般问题不需要询问是否需要更多信息 - 人们会问他们是否需要更多信息。请记住,问题永远存在于此,因此它们需要相当简洁)。跨度>
-
@JibяaᴎKhaᴎ 起初我不是,现在如果你检查我在我的问题中所做的更新,我就是
-
@ZahanSafallwa 没有显示错误 :( 很困惑
标签: android imageview android-imageview android-image