无法将位图图像获取到图像视图

Posted

技术标签:

【中文标题】无法将位图图像获取到图像视图【英文标题】:Unable to get bitmap image to image view 【发布时间】:2017-03-08 00:38:07 【问题描述】:

图像保存在所需的文件夹中,但它没有显示在 imageView 中,也没有显示 Toast。我尝试了几乎所有方法,例如使用不同的 XML 文件和更改类,但问题仍然存在。

package com.example.mohammadhasham.jtbopencameramodule;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Date;

public class CameraActivity extends AppCompatActivity 

    ImageView img;
    String currentPath;
    File f;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        img = (ImageView) (findViewById(R.id.capture));


    

    public void captureImage(View v) 

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "image.jpg");
        Uri u = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, u);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(i, 0);
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 

        if (resultCode == 0) 
            switch (resultCode) 
                case Activity.RESULT_OK:
                
                    if (f.exists())
                    
                        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();
                         Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());
                          img.setImageBitmap(b);


                    
                    else
                    
                        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

                    
                    break;
                
                case Activity.RESULT_CANCELED:
                
                    break;
                
                default:
                    break;
            
        
    


【问题讨论】:

The image is saved in the desired folder。你的帖子开头很奇怪。哪个图?告诉从一开始会发生什么。之后显示您的代码。 【参考方案1】:

我相信你的代码有错误:

你说

if (resultCode == 0) 
            switch (resultCode) 
                case Activity.RESULT_OK:
                

您将 resultCode 限制为值 0,然后尝试根据它的值执行切换。

【讨论】:

我真的很笨!浪费了一个小时!谢谢,真的有效。【参考方案2】:

试试这个:

公共类 CameraActivity 扩展 AppCompatActivity

private final String APP_TAG = "MyCustomApp";
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String photoFileName = "photo.jpg";

private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    img = (ImageView) (findViewById(R.id.imageView));


public void captureImage(View v) 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
        takePictureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();

        Uri takenPhotoUri = getPhotoFileUri(photoFileName);
        Bitmap imageBitmap = BitmapFactory.decodeFile(takenPhotoUri.getPath());
        img.setImageBitmap(imageBitmap);
     else 
        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

    


// Returns the Uri for a photo stored on disk given the fileName
public Uri getPhotoFileUri(String fileName) 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) 
        Log.d(APP_TAG, "failed to create directory");
    

    // Return the file target for the photo based on filename
    return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName));

【讨论】:

以上是关于无法将位图图像获取到图像视图的主要内容,如果未能解决你的问题,请参考以下文章

如何从可绘制文件夹上的图像获取路径并设置为图像视图,位图?

如何在android中使用imageloader释放位图内存?

将位图保存到文件中并返回具有位图图像的文件

为啥使用矩阵到图像视图与位图的结果不同?

在 Picasso 图像加载器中加载位图图像会减慢列表视图中的滚动速度

将位图图像加载到按钮