无法使用onActivityResult显示图像

时间:2016-02-25 00:48:00

标签: android

大家

我的代码有问题,我不知道出了什么问题。 我只想用原生相机拍照并将其显示在ImageView中。

这是我的代码

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_IMAGE_CAPTURE);




@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CAMERA_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap m = null;
        try {
            m = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
        imgActa1.setImageBitmap(m);
    }

在Galaxy Tab上一切正常。在我的Galaxy S4中,imageview保持为null,而在Alcatel OneTouch上,该应用程序会抛出异常。

我该怎么办?

感谢。

3 个答案:

答案 0 :(得分:0)

使用此代码

 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);
        }  
    }

希望这可以帮助你

答案 1 :(得分:0)

事情已经改变了更高版本的sdk, 你可以用这个代码来获取imgae     @覆盖     public void onActivityResult(int requestCode,int resultCode,Intent data){         super.onActivityResult(requestCode,resultCode,data);

    if (resultCode == RESULT_OK) {
        if (requestCode == RESULT_LOAD_IMAGE) {
            Uri selectedImageUri = data.getData();
            if (Build.VERSION.SDK_INT < 19) {
                String selectedImagePath = getPath(selectedImageUri);
                Bitmap bitmap = decodeSampledBitmapFromPath(selectedImagePath,150,150);
                icon.setImageBitmap(bitmap);
            }
            else {
                ParcelFileDescriptor parcelFileDescriptor;
                try {
                    parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
                    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                    Bitmap image = decodeSampledBitmapFromFd(fileDescriptor, 150, 150);
                    parcelFileDescriptor.close();
                    icon.setImageBitmap(image);
                    prefEditor.putString(Constants.IMAGETEXT, ConvertImageToBase64.convertResource(image));
                    prefEditor.commit();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

    public static Bitmap decodeSampledBitmapFromFd(FileDescriptor fileDescriptor,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor, new Rect(-1, -1, -1, -1) , options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(fileDescriptor, new Rect(-1, -1, -1, -1), options);
}

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

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( path,options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

答案 2 :(得分:0)

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

    super.onActivityResult(requestCode, resultCode, data);
    ///*
    if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data){


        uri = data.getData();

        String[] prjection ={MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(uri,prjection,null,null,null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(prjection[0]);

        ImagePath = cursor.getString(columnIndex);

        cursor.close();

        FixBitmap = BitmapFactory.decodeFile(ImagePath);

        ShowSelectedImage = (ImageView)findViewById(R.id.imageView);



        ShowSelectedImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));

    }
}

希望这可以帮助你