【问题标题】:SetImageBitmap shows white blank full screenSetImageBitmap 显示白色空白全屏
【发布时间】:2016-10-31 16:05:14
【问题描述】:

我有一个打开 CameraActivity 的 CardActivity。我按下了一个图像按钮,然后本地相机应用程序打开,我可以拍照。我尝试使用带有 ByteArray 的意图将其传递回我的 CardActivity。但它给了我一个空白的白色屏幕。它不会在图像视图中插入任何内容。 Bitmap 元素不为空,它有一些东西。

这是我启动相机活动和设置图像的开关:

switch (v.getId()) {
                case R.id.pButton1:
                    Intent cam = new Intent(CardActivity.this, CameraActivity.class);
                    startActivity(cam);
                    returnImage2();
                    mImageView = (ImageView)findViewById(R.id.imageView);
                    mImageView.setImageBitmap(bitmap);
                    mImageView.setRotation(180);
                    break;

这是我的 returnImage2();

public void returnImage2() {

        try {
            bitmap = BitmapFactory.decodeStream(this.openFileInput("myImage"));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的cameraActivity:

    public class CameraActivity extends Activity {
        private String mCurrentPhotoPath;
        private ImageView mImageView;
        private Bitmap mImageBitmap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            dispatchTakePictureIntent();
        }
        public void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    System.out.println("ERR CANNOT CREATE FILE");
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(this,
                            "com.example.android.fileprovider",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
        private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = "file:" + image.getAbsolutePath();
            return image;
        }
        private File createImageFile2() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = "file:" + image.getAbsolutePath();
            return image;
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                try {
                    mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
                    cImageFromBitmap(mImageBitmap);
                    //mImageView = (ImageView)findViewById(R.id.imageView);
                    //mImageView.setImageBitmap(mImageBitmap);
                    //mImageView.setRotation(180);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        public String cImageFromBitmap(Bitmap bitmap){
            String fileName = "myImage";//no .png or .jpg needed
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
                fo.write(bytes.toByteArray());
                // remember close file output
                fo.close();
            } catch (Exception e) {
                e.printStackTrace();
                fileName = null;
            }
            return fileName;
        }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    我注意到您正在使用REQUEST_TAKE_PHOTO 开始您的活动

                 startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
    

    但您正在 onActivityResult 上检查 REQUEST_IMAGE_CAPTURE

                 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
    

    还有:

    我不知道你为什么要经历所有这些工作来创建一个文件。当您使用ACTION_IMAGE_CAPTURE 时,位图本身会在结果意图中返回:

                if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
                    try {
                        mImageBitmap = (Bitmap) data.getExtras().get("data");
    

    【讨论】:

    • 我完成所有这些工作的原因是为了处理超过 1 mb 的意图:/ 如果我删除了 if 检查的那部分,我仍然会得到白屏
    猜你喜欢
    • 2013-02-14
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多