Imageview拒绝显示图像(Imageview refuses to display image)

编程入门 行业动态 更新时间:2024-10-06 22:19:46
Imageview拒绝显示图像(Imageview refuses to display image)

我已经尝试了很多不同的方法来在我的imageView中显示图片,但它只是拒绝这样做。 我不知道发生了什么事。 我的应用程序需要做的不仅仅是拍照并展示它。 基本上所有这些代码都直接来自Android网站 。

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(); } }

}

我的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" />

}

My mCurrentPhotoPath is no longer null. Image STILL doesn't show up. All pictures I've taken are in my phone's gallery though, so it is saving. The xml for the imageView in question is here:

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" />

I have the following line in my onCreate:

mImageView = (ImageView) findViewById(R.id.main_picture);

最满意答案

如果你需要用相机拍照并在图像视图中显示(这是我所理解的)我认为这可以帮助你

从相机中捕获图像并在活动中显示

编辑:把所有适合我的代码

调用意图

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(); } } }

希望这可以帮到你。

If you need to take a photo with the camera and display it in image view (which is what i understand) i think that this can help you

Capture Image from Camera and Display in Activity

EDIT: Put the all code that work for me

Call the intent

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); } } }

Method that create the file

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; }

Method that save and restore the state of the 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); } }

The activity result (your setPic() method)

@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(); } } }

hope this can help you.

更多推荐

本文发布于:2023-08-07 12:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Imageview   refuses   image   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!