android - 使用 ACTION_IMAGE_CAPTURE 拍照并 setImageBitmap 来显示它

标签 android image bitmap imageview

编辑我更新了代码以反射(reflect)两个答案中建议的更改,不幸的是,现在我的应用程序强制关闭。错误列在底部

这是我的整个相机/图片类(除了 imports),该类应该拍摄一张照片,将其显示到屏幕上,并让另一个类具有以下字符串路径用作附件的图片。 它拍摄的照片很好,它通过电子邮件发送的照片也很好,但我不知道如何让它在拍摄后显示照片。我知道其中有一些草率的代码(例如在 onActivityResult 中),但除了

Bitmap bm = BitmapFactory.decodeFile(image_string);
imageview.setImageBitmap(bm);

效果很好,所以我对寻找解决方案非常感兴趣。您对如何清理此问题提出的任何其他建议也非常受欢迎,这只是从多个不同来源拼凑在一起的。

public class LooksLike extends Activity
    {       
    Button camera;
    Intent intent;
    static String image_string;
    ImageView imageview;
    public void onCreate (Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lookslike);
        camera = (Button) findViewById(R.id.camera);
        imageview = (ImageView) findViewById(R.id.debris_view);
        camera.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View v)
                {
                takePhoto();
                }
            });
        }

    public void takePhoto()
        {
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
        startActivityForResult(intent, 1);
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
        }

    private File getTempFile(Context context)
        {
        File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
        if(!path.exists())
            path.mkdir();
        return new File(path, "debris.jpg");
        }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    if (resultCode == RESULT_OK)
        {
        switch(requestCode)
            {
            case 1:
            final File file = getTempFile(this);
            try
                {
                Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
                image_string = Uri.fromFile(file).toString();
                Bitmap bm = Bitmap.createScaledBitmap(
                    BitmapFactory.decodeFile(image_string),
                    getResources().getDisplayMetrics().widthPixels,
                    getResources().getDisplayMetrics().heightPixels, 
                    true);
                imageview = (ImageView) findViewById(R.id.debris_view);
              imageview.setImageBitmap(bm);
                }
            catch (FileNotFoundException e)
                {
                Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
                }
            catch (IOException e)
                {
                Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
                }
            break;
            }
        }
    }
}

另外,这是我的 xml 文件:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView  
        android:id="@+id/debris_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/icon"
        />
    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="would you like to take a new debris picture?"
        />
</merge>

最佳答案

您需要将这两行移动到 onActivityResult() 中:

case 1:
    final File file = getTempFile(this);
    try {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        image_string = Uri.fromFile(file).toString();
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
    } catch (FileNotFoundException e) {
        ...

takePhoto() 中的代码将在 startActivityForResult() 之后立即执行(即,它不会等待调用的 Activity 完成)。处理结果必须在onActivityResult()中完成。

关于android - 使用 ACTION_IMAGE_CAPTURE 拍照并 setImageBitmap 来显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231645/

相关文章:

android - Lazy List 上的位图缓存(SoftReference,Hard)似乎无法正常工作 - Android

android - 缺少 Google Play 私有(private) channel "Restrict Distribution"选项

Python OpenCV线条检测检测图像中的 `X`符号

css - 仅在 html css 中将缩略图的背景图像网格放大到更高分辨率

css - 悬停时不需要的重复图像

c# - 无法在位图中设置调色板

android - 延迟显示图像在android中使用位图

Android:GPS 从精细到粗糙的回退

Android SQLite 列 int 转为 float 可能吗?

android - 导入原生android源码到android studio并构建