相机图像未显示在ImageView中

时间:2015-06-29 03:22:09

标签: android android-camera

我想在ImageView中显示来自Camera Intent的捕获图像。我可以让相机打开并为图像创建一个目录,但是当我通过意图传递图像并尝试调用它时,图像不会显示。这就是我在做的事情:

private static final int REQUEST_CODE = 1;
private ImageView addPicture;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(//setting the view);

    addPicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
            imagesFolder.mkdirs(); // <----
            File image = new File(imagesFolder, "image_001.jpg");
            Uri uriSavedImage = Uri.fromFile(image);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            startActivityForResult(imageIntent, 0);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    addPicture = (ImageView) findViewById(R.id.profilepicture_registerone);

    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            try {
                InputStream stream = getContentResolver().openInputStream(data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                addPicture.setImageBitmap(bitmap);
                stream.close();


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {

            }

            RoundImage image = new RoundImage(bitmap);

        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            Toast.makeText(getApplicationContext(), "Bad Image Request. Please Try Again!", Toast.LENGTH_LONG).show();
        }
    }

}

我的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" />

我的ImageView in XML:

<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/profilepicture_registerone"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:clickable="true"
    android:onClick="addPicture" />

编辑:由于我试图更改此帖子的内容,我的ID不正确。我将它们更改回原始ID以避免混淆。

编辑2:经过进一步测试后,我发现onActivityResult()方法中没有设置图像。我尝试加载一个可绘制的图像,但也无法正常工作。当我在onCreate()内部调用它时,它工作正常。

3 个答案:

答案 0 :(得分:1)

您需要在onCreate()中找到具有正确ID标记的ImageView ID。

 addPicture = (ImageView) findViewById(R.id.profilepicture_registerone);

请检查您的位图,是否为空?

答案 1 :(得分:1)

试试这个,

在类区域中声明常量:

final int TAKE_PHOTO_REQ = 100;

调用相机意图:

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, TAKE_PHOTO_REQ);

将onActivityResult(..)添加到您的活动

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PHOTO_REQ: {
            if (resultCode == TakePicture.RESULT_OK && data != null) {

                Bitmap myBmp = (Bitmap) data.getExtras().get("data");



                imageView.setImageBitmap(myBmp);
                break;
            }
        }
        }
    }

*仍有问题,请检查 ImageView ,如果它是否正确。

希望这会有所帮助......谢谢

答案 2 :(得分:0)

您确定,您正在使用imageview的正确ID。在代码中,xml中的id是“picture”,但你在java中引用profilepicture_registerone?