通过电子邮件发送位图而不将其保存在设备上 - Android

时间:2016-07-19 15:01:42

标签: java android email bitmap

我正在创建一个Android应用程序,我需要发送相机拍摄的照片,但出于安全考虑,我无法将此照片保存在用户设备中。

我发现的有关通过电子邮件发送位图的每个解决方案似乎都将其存档在应用程序的目录中,然后使用它的路径发送它。

在我的应用程序中,图像仅用于此活动(以及通过电子邮件发送时)。

Soo ......无论如何发送Bitmap而不保存它?

编辑: 这就是我现在正在努力做到这一点(它没有正常工作)。

调用相机:

imageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //Opens camera app
        foto          = true;
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
    }
});

获得结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Converts picture to PNG
    if ((requestCode == CAMERA_PIC_REQUEST) && (resultCode == RESULT_OK)) {
        thumbnail       = (Bitmap)    data.getExtras().get("data");
        ImageView image = (ImageView) findViewById(R.id.image_comment);
        image.setImageBitmap(thumbnail);

        try {
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
                // We + "/MapTest" to make it storage on a deeper directory for our application
                problemPicture       = new File(getCacheDir(), "problemPicture.png");
                FileOutputStream out = new FileOutputStream(problemPicture);
                thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            Log.e("BROKEN", "Could not write file " + e.getMessage());
        }
    }
}

发送电子邮件(foto在它之前设置为true):

public void sendEmail(){
        Intent i = new Intent(Intent.ACTION_SEND);
        if(foto) { //if user attached a pic
            i.setType("image/png");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"sample_mail@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Assistencia Tecnica");
            i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(problemPicture));
            i.putExtra(Intent.EXTRA_TEXT, commentText.getText());
            problemPicture.delete();
            try {
                startActivity(Intent.createChooser(i, "Enviando Email..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(StatusActivity.this, "Comunicaçaõ Falhou",
                        Toast.LENGTH_SHORT).show();
            }
            try {
                // We try to delete the picture
                boolean success = problemPicture.delete();
                if(success) {
                    Log.i(TAG, "File deleted successfully");
                } else {
                    if(problemPicture.isDirectory()) {
                        success = deleteDirectory(problemPicture);
                        if(success) {
                            Log.i(TAG, "Directory deleted successfully!");
                        } else {
                            Log.i(TAG, "Could not delete directory");
                        }
                    }
                    Log.i(TAG, "File could not be deleted");
                }
            } catch (NullPointerException e) {
                // In case we don't find it
                Log.i(TAG, "File not found");
            }

            try {
                deleteFile("problemPicture.png");
            } catch(NullPointerException e) {
                Log.i(TAG, "Context file not found");
            }
        }
        else {
            i.setType("plane/text");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"sample_email@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Assistencia Tecnica");
            i.putExtra(Intent.EXTRA_TEXT, commentText.getText());
            try {
                startActivity(Intent.createChooser(i, "Enviando Email..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(StatusActivity.this, "Comunicaçaõ Falhou", Toast.LENGTH_SHORT).show();
            }
        }
    }

结束活动

private void endActivity() {
    //Back to Maps Activity
    boolean success = false;
    try {
        success = problemPicture.delete();
    } catch(NullPointerException e) {
        Log.d(TAG, "No picture found");
    }
    if(success) {
        Log.i(TAG, "Picture deleted");
    } else {
        Log.i(TAG, "Picture could not be deleted");
    }

    success = deleteFile("problemPicture.png");
    if(success) {
        Log.d(TAG, "File deleted");
    } else {
        Log.d(TAG, "File could not be deleted");
    }

    Toast.makeText(getApplicationContext(), "OS Finalizada",Toast.LENGTH_SHORT).show();
    Intent it = new Intent(StatusActivity.this, MapsActivity.class)
            .putExtra("index", index);
    startActivity(it);
}

对不起代码墙感到抱歉,谢谢!

2 个答案:

答案 0 :(得分:0)

对于共享图像,我们通过意图将图像的URI发送到相应的应用程序。没有使用URI就没有可能发送它。 URI要求必须保存图像。所以没有可能。如果您在共享后有删除它的问题....更好地保存在DiskCache中,以便根据LRU自动删除。

答案 1 :(得分:0)

private static final int TAKE_PICTURE = 1;

    public void takePhoto(View view) {

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, TAKE_PICTURE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case TAKE_PICTURE:
                    if (resultCode == Activity.RESULT_OK) {
                        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                        Uri uri = data.getData();
//                      ImageView image = (ImageView) findViewById(R.id.image_comment);
//                      image.setImageBitmap(thumbnail);
                        // set image if you want to

                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM,
                                uri);
                        shareIntent.setType("image/jpeg");
                        startActivity(Intent.createChooser(shareIntent,
                                "choose one"));
                        // your desired intent with your own parames subject and others 
                        // no need to del and others.
                        // this is test in API 19 hope it will work for you too!

                    }
                    break;
            }
        }