android - Android 中用相机拍照并将其发送到邮件

标签 android

我想编写一个仅具有此功能的应用程序: - 使用手机摄像头拍摄图像 - 将其作为邮件附件发送到给定地址

我写了下面的代码,我不明白为什么我无法退出相机模式(按接受按钮没有效果)

public class MainActivity extends Activity {
  private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private File f;
    public File getAlbumDir()
    {

        File storageDir = new File(
                Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES
                ), 
                "BAC/"
            ); 
        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;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.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();
                }



            }
        });
    }

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

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583e312a2b2c7634392b2c183f35393134763b3735" rel="noreferrer noopener nofollow">[email protected]</a>"});
            i.putExtra(Intent.EXTRA_SUBJECT, "first picture");
            i.putExtra(Intent.EXTRA_TEXT   , "body of email");

            Uri uri = Uri.fromFile(f);
            i.putExtra(Intent.EXTRA_STREAM, uri);
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
        }  
    } 
}

如有任何建议,我将不胜感激! 谢谢!

最佳答案

相机无法返回,因为您的相册目录不存在。尝试使用

public File getAlbumDir() {
    File storageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "BAC/"
    );

    // Create directories if needed
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }

    return storageDir;
}

此外,由于您提供了 EXTRA_OUTPUT 额外内容,因此 onActivityResult() 中的参数 data 可能为 null 。您可以从 File 对象构建位图

Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());

关于android - Android 中用相机拍照并将其发送到邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993459/

相关文章:

java - 如何通过从 ListView 或 android 中的微调器中选择选项来更改应用程序主题?

java - 数组 [i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

android - 错误 :Execution failed for task ':app:transformClassesWithJarMergingForRelease'

不独立滚动的Android多个 ListView

android - 无法在 Android 中保存 parseobject

Android 应用程序不会在 Play 商店中更新

java - 如何通过java以键值对的形式在android中添加复选框?

android - 如何在 Android 的 ViewPager 中的 fragment 中播放 Youtube 视频

java - Java 中 Intent 之间的困难 (Android Studio)

android - ImageView 太小