Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用相机拍照并将其发送到Android中的邮件_Android - Fatal编程技术网

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

用相机拍照并将其发送到Android中的邮件,android,Android,我想编写一个仅具有以下功能的应用程序: -使用手机摄像头拍摄图像 -将其作为邮件附件发送到给定地址 我已经写了以下代码,我不明白为什么我不能退出相机模式(按下接受按钮没有效果) 如果有任何建议,我将不胜感激! 谢谢 相机不会返回,因为您的相册目录不存在。试一试 public File getAlbumDir() { File storageDir = new File( Environment.getExternalStoragePublicDirectory(E

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

我已经写了以下代码,我不明白为什么我不能退出相机模式(按下接受按钮没有效果)

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

相机不会返回,因为您的相册目录不存在。试一试

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\u输出
EXTRA,因此
onActivityResult()中的参数
data
可能是null。您可以从
文件
对象生成位图

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

对于存在相同问题的任何人,更正后的功能代码如下:

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

    @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 = BitmapFactory.decodeFile(f.getAbsolutePath());
            imageView.setImageBitmap(photo);

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"raul.pop90@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Prima poza");
            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 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/"
            ); 
         // 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;
    }

    @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 = BitmapFactory.decodeFile(f.getAbsolutePath());
            imageView.setImageBitmap(photo);

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"raul.pop90@gmail.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Prima poza");
            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();
            }
        }  
    } 
}