【问题标题】:display picture in imageview photo taken by camera in android在android中相机拍摄的imageview照片中显示图片
【发布时间】:2012-12-03 11:49:22
【问题描述】:

我在使用相机进行 Android 开发时遇到问题。我尝试拍照并将其显示在 imageview 中,但结果未显示,但它适用于 Galaxy Note,但不适用于 Galaxy S3

我的代码如下

相机活动

package com.nfs;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Camera extends Activity {
    private static final int CAMERA_REQUEST = 500;
    private ImageView imageView;

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

    this.imageView = (ImageView) findViewById(R.id.imgLoad);
    Button bt = (Button) findViewById(R.id.btCam);

    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    startActivityForResult(intent, CAMERA_REQUEST);                         

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btCam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Click Here" />

    <ImageView
        android:id="@+id/imgLoad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.70"
        android:src="@drawable/icon" />
</LinearLayout>

【问题讨论】:

  • 有什么异常吗?你能从onActivityResult 方法中的意图中获取位图吗...
  • 不,没有例外,图像显示敌人一秒钟然后消失

标签: android android-4.2-jelly-bean


【解决方案1】:

您好,试试我的代码,它运行良好。我想这会解决您的问题。链接如下:-Imp Link

【讨论】:

    【解决方案2】:

    试试这个:

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            final Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
        switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {
                try {
                    try {
                        Uri selectedImage = imageReturnedIntent.getData();
                        bmpSelectedImage = getThumbnail(selectedImage);
    
                        set_img_camera.setImageBitmap(bmpSelectedImage);
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                } catch (Exception e) {
                    Log.d("image error", e.toString());
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多