照片不显示在图像视图android

时间:2014-10-21 23:43:14

标签: android android-intent camera

所以昨晚我认为我的应用程序工作了,但是,今天早上gremlins已经入侵,现在我的应用程序的功能无法正常工作。

基本上,一个按钮允许用户拍摄照片,并将照片显示在Imageview中,然后将图像附加到电子邮件中..它可以让我拍摄照片,它仍然作为附件出现,但是图像视图中的预览完全为空。

有人能看到发生了什么吗?

Hoon_Image = (ImageView) findViewById(R.id.CapturedImage);
        button_take_photo = (Button)findViewById(R.id.btn_take_photo);
        button_take_photo.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                try {
                    f = createImageFile();
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });
public File getAlbumDir()
    {

        File storageDir = new File(
                Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES
                ),
                "BAC/"
        );
        // Create directories if needed
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

        return storageDir;
    }
    private File createImageFile() throws IOException {
        // Create an image file name

        String imageFileName =getAlbumDir().toString() +"/image.jpg";
        File image = new File(imageFileName);
        return image;
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(requestCode == CAMERA_REQUEST ){
                Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
                Hoon_Image.setImageBitmap(photo);
            }
        }
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();

                //OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();

                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);

                //DEBUG PURPOSE - you can delete this if you want
                if(selectedImagePath!=null)
                    System.out.println(selectedImagePath);
                else System.out.println("selectedImagePath is null");
                if(filemanagerstring!=null)
                    System.out.println(filemanagerstring);
                else System.out.println("filemanagerstring is null");

                //NOW WE HAVE OUR WANTED STRING
                if(selectedImagePath!=null)
                    System.out.println("selectedImagePath is the right one for you!");
                else
                    System.out.println("filemanagerstring is the right one for you!");
            }
            Bitmap photo = BitmapFactory.decodeFile(selectedImagePath);
            Hoon_Image.setImageBitmap(photo);
            Photo_Selected = 1;
        }
    }

2 个答案:

答案 0 :(得分:0)

我在Android中的ImageView中显示的图像是使用方法decodeSampledBitmapFromFile(String path,int reqWidth,int reqHeight)来解码具有所需宽度和高度的File对象。以下是示例代码。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_OK) {
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "image.jpg");
        bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(),
                600, 450);
        imageView.setImageBitmap(bitmap);

    }
}

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    // Query bitmap without allocating memory
    options.inJustDecodeBounds = true;
    // decode file from path
    BitmapFactory.decodeFile(path, options);
    // Calculate inSampleSize
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    // decode according to configuration or according best match
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;
    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;
    if (expectedWidth > reqWidth) {
        // if(Math.round((float)width / (float)reqWidth) > inSampleSize) //
        // If bigger SampSize..
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }
    // if value is greater than 1,sub sample the original image
    options.inSampleSize = inSampleSize;
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

答案 1 :(得分:0)

所以我发现我做错了什么 - derp时刻即将到来。

这部分代码:

if (resultCode == RESULT_OK) {
            if(requestCode == CAMERA_REQUEST ){

它只是看起来很好的结果(这既发生了选择照片并成功拍摄照片),而不是首先寻找RequestCode。

重新安排代码到此工作:

if(requestCode == CAMERA_REQUEST ) {
            if (resultCode == RESULT_OK){
                Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
                Hoon_Image.setImageBitmap(photo);