图像从Android App中的ImageView中消失

时间:2015-09-05 16:17:18

标签: java android

我对Android和Java都是新手(只有一些基本知识)。我正在尝试根据youtube video开发一个简单的应用程序。在这个应用程序中,我有一个可点击的按钮,并调用方法launchCamera()。相机拍摄的图像必须显示在ImageView中。

问题:我在手机中安装了.apk文件。当我点击"拍照"按钮,我的相机启动。当我从相机捕获图像并保存图像时,该图像仅在ImageView显示一秒钟(甚至不到一秒钟)。如何在ImageView中保留该照片,直到用户没有按下"拍照"按钮再次?

更新:使用相同的代码,我发现了一些奇怪的东西。大多数情况下,垂直拿着手机拍摄的图像会在ImageView内自行旋转并消失。但有时它会垂直停留在ImageView并且不会消失,直到"拍照"再次按下按钮(所需的情况)。

我的代码:

package com.siddexample.buttonimage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.content.pm.PackageInfo;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ButtonImageMainActivity extends AppCompatActivity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    ImageView siddImageView;

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

        Button siddButton = (Button) findViewById(R.id.siddButton);
        siddImageView = (ImageView) findViewById(R.id.siddImageView);
    }//////-------------///////////////


    public void launchCamera(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Take a picture by your intent
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap photo = (Bitmap) extras.get("data");
            siddImageView.setImageBitmap(photo);

        }
    }

}

1 个答案:

答案 0 :(得分:-1)

尝试使用本教程here,如下所示:

拍照:

private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

收到图片:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

并显示图片:

/*
     * Display image from a path to ImageView
     */
    private void previewCapturedImage() {
        try {
            // hide video preview
            videoPreview.setVisibility(View.GONE);

            imgPreview.setVisibility(View.VISIBLE);

            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 8;

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);

            imgPreview.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

正如@stkent所说,也许onActivityResult会被调用两次。