1

我的要求是从相机拍照或从图库中选择图像并将其显示在图像视图中。显示图像后,无论选择何种图像,我都将作为邮件发送。我的问题是每次我从它旋转的相机中拍摄肖像照片并在图像视图中显示它。如何解决这个问题。

提前致谢。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.skicka_bild_activity);

        share = new Sharedprefs(SkickaBild.this);

        emailSendButton = (Button) findViewById(R.id.skicka_button);
        emailSendButton.setOnClickListener(this);

        emailSendButton.setEnabled(false);

        Button camaraOnButtton = (Button) findViewById(R.id.fotografera_button);
        camaraOnButtton.setOnClickListener(this);

        Button galleryOpenButton = (Button) findViewById(R.id.valj_bild_button);
        galleryOpenButton.setOnClickListener(this);

        imageSetImageView = (ImageView) findViewById(R.id.imageView_skick);

        maxWidth = share.getmaxwidth();

        mContentResolver = getContentResolver();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.skicka_button:

            if (isImageViewFilled) {

                showEmailComposer();
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        SkickaBild.this);
                builder.setTitle("Alert !!");
                builder.setMessage("Pick a image before you send the email")
                        .setCancelable(false)
                        .setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();

                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();
            }

            break;

        case R.id.fotografera_button:

            share.setisFromTabsClearImage(false);
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            Intent takePictureIntent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    mCapturedImageURI);
            startActivityForResult(takePictureIntent, 0);

            break;
        case R.id.valj_bild_button:

            share.setisFromTabsClearImage(false);

            Intent pickPhoto = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(pickPhoto, 1);

            break;
        }

    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK) {


                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(mCapturedImageURI,
                        projection, null, null, null);
                int column_index_data = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String capturedImageFilePath = cursor
                        .getString(column_index_data);

                Bitmap mImageBitmap = BitmapFactory
                        .decodeFile(capturedImageFilePath);
                imageSetImageView.setImageBitmap(mImageBitmap);

                isImageViewFilled = true;

                if (isImageViewFilled) {
                    emailSendButton.setEnabled(true);

                    emailSendButton.setBackgroundDrawable(getResources()
                            .getDrawable(R.drawable.skicka_button_black));

                }

                File root = Environment.getExternalStorageDirectory();
                if (root.canWrite()) {
                    System.out.println("PICTURE PATH " + capturedImageFilePath);
                    pic = new File(capturedImageFilePath);

                    String cPath = pic.getAbsolutePath();


                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(cPath, bmOptions);
                    float photoW = mImageBitmap.getWidth();
                    float photoH = mImageBitmap.getHeight();

                    // Get the dimensions of the View
                    float targetW = Float.parseFloat(maxWidth);
                    float targetH = (photoH / photoW) * targetW;

                    System.out.println("cam bitmap height"
                            + mImageBitmap.getHeight());
                    System.out.println("cam bitmap weidth" + maxWidth);

                    System.out.println("cam bitmap Width 02 "
                            + mImageBitmap.getWidth());

                    System.out.println("cam target Height " + targetH);
                    System.out.println("cam target width " + targetW);

                                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = (int) scaleFactor;
                    bmOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(cPath, bmOptions);
                    mImageBitmap = Bitmap.createScaledBitmap(bitmap,
                            (int) targetW, (int) targetH, false);

                    FileOutputStream fout;
                    try {
                        fout = new FileOutputStream(pic);
                        mImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                                fout);
                        fout.flush();
                        fout.close();
                        bitmap.recycle();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }

            break;
        case 1:
            if (resultCode == RESULT_OK) {

                Uri selectedImage = imageReturnedIntent.getData();

                InputStream in = null;
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                try {

                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);

                    System.out.println("PICTUREPATH" + picturePath);
                    cursor.close();

                    Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);

                    int orientation = getOrientation(this, selectedImage);
                    System.out.println("ORIENTATION VALUE " + orientation);
                    imageSetImageView.setImageBitmap(imageBitmap);

                    isImageViewFilled = true;

                    if (isImageViewFilled) {

                        emailSendButton.setEnabled(true);

                        emailSendButton.setBackgroundDrawable(getResources()
                                .getDrawable(R.drawable.skicka_button_black));

                    }

                    // ////////////////////////// Image is set ///////////////

                    pic = new File(picturePath);

                    String cPath = pic.getAbsolutePath();
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    bmOptions.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(cPath, bmOptions);
                    float photoW = imageBitmap.getWidth();
                    float photoH = imageBitmap.getHeight();

                    // Get the dimensions of the View
                    float targetW = Float.parseFloat(maxWidth);
                    float targetH = (photoH / photoW) * targetW;

                    System.out.println(" bitmap height"
                            + imageBitmap.getHeight());
                    System.out.println(" bitmap weidth" + maxWidth);

                    System.out.println(" bitmap Width 02 "
                            + imageBitmap.getWidth());

                    System.out.println(" target Height " + targetH);
                    System.out.println(" target width " + targetW);

                                        float scaleFactor = Math.min(photoW / targetW, photoH
                            / targetH);

                    bmOptions.inJustDecodeBounds = false;
                    bmOptions.inSampleSize = (int) scaleFactor;
                    bmOptions.inPurgeable = true;

                    Bitmap bitmap = BitmapFactory.decodeFile(cPath, bmOptions);
                    imageBitmap = Bitmap.createScaledBitmap(bitmap,
                            (int) targetW, (int) targetH, false);

                    FileOutputStream fout;

                    fout = new FileOutputStream(pic);
                    imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                    fout.flush();
                    fout.close();

                    bitmap.recycle();
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            break;
        }

    }

    public static int getOrientation(Context context, Uri photoUri) {
        String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
        Cursor cursor = context.getContentResolver().query(photoUri,
                orientationColumn, null, null, null);

        try {
            if (cursor.moveToFirst()) {
                return cursor.getInt(cursor
                        .getColumnIndex(orientationColumn[0]));
            } else {
                return -1;
            }
        } finally {
            cursor.close();
        }
    }


    private void showEmailComposer() {

        Intent email = new Intent(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL, new String[] { share.getMailTo() });

        email.putExtra(Intent.EXTRA_SUBJECT, share.getsubject());

        email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
        // email.putExtra(Intent.EXTRA_STREAM, pic.getName());
        email.putExtra(Intent.EXTRA_TEXT, share.getemailbody());

        email.setType("message/rfc822");

        startActivity(Intent.createChooser(email, "Choose an Email client :"));

        isEmailSend = true;

    }

    @Override
    protected void onResume() {

        super.onResume();

        System.out.println("ON RESUME INCOMING CHECK");

        if (share.getisFromTabsClearImage()) {

            imageSetImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.ingenbildvald));

            emailSendButton.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.skicka_button));

            emailSendButton.setEnabled(false);
            share.setisFromTabsClearImage(false);

        }
        if (isEmailSend) {

            imageSetImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.ingenbildvald));

            emailSendButton.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.skicka_button));
            emailSendButton.setEnabled(false);

            isEmailSend = false;
        }
    }
4

0 回答 0