无法通过电子邮件发送的意图图片附件 [英] Can't send image attachment by email intent

查看:178
本文介绍了无法通过电子邮件发送的意图图片附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过附件电子邮件的意图发送图像。
我选择的Gmail应用程序,它似乎文件附加,但是当我点击发送的Gmail应用程序它说:


  

很抱歉,Gmail已经停止。


请帮我什么问题,我该如何解决?

的Andr​​oidManifest.xml:

 <使用许可权的android:NAME =android.permission.CAMERA/><使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

MyActivity.java:

 公共类MyActivity延伸活动{私有静态最终诠释SELECT_PICTURE = 1;
私有静态最终诠释CAPTURE_IMAGE_ACTIVITY_REQUEST_ code = 2;
私人字符串selectedImagePath;
TextView的uritv = NULL;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    uritv =(的TextView)findViewById(R.id.uritxt);    按钮发送=(按钮)findViewById(R.id.sendBtn);
    send.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            如果(uritv.getText()。的toString()。等于(URI)){
                Toast.makeText(getApplicationContext(),请首先选择的图像!,Toast.LENGTH_SHORT).show();
            }其他{
                意图emailIntent =新意图(android.content.Intent.ACTION_SEND);
                emailIntent.setType(图像/ *);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新的String [] {myemail@myemail.com});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,测试对象);
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,从我的应用程序);
                emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(uritv.getText()的toString())。);
                startActivity(Intent.createChooser(emailIntent,发送邮件...));
            }
        }
    });    按钮galbtn =(按钮)findViewById(R.id.galBtn);
    galbtn.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            意向意图=新的Intent();
            intent.setType(图像/ *);
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(意向,
                    选择图片),SELECT_PICTURE);
        }
    });    按钮cambtn =(按钮)findViewById(R.id.camBtn);
    cambtn.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            意向意图=新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
            startActivityForResult(Intent.createChooser(意向,选择图片),CAPTURE_IMAGE_ACTIVITY_REQUEST_ code);
        }
    });}
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
        如果(要求code == || SELECT_PICTURE要求code == CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
            乌里selectedImageUri = data.getData();
            selectedImagePath =的getPath(selectedImageUri);
            uritv.setText(selectedImagePath.toString());
        }
    }
}
公共字符串的getPath(URI URI){
    的String [] =投影{MediaStore.Images.Media.DATA};
    光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
    INT与Column_Index =光标
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    返回cursor.getString(Column_Index中);
}
}


解决方案

是的,问题是与您建立得到文件路径的URI。您正在服用哪些是不符合URI的情况下,原始文件的路径。它应该是像下面

 内容://媒体/外部/图像/媒体

请检查您的路径,并再次测试该

I have tried to send image by attachment in email intent. I select gmail app, it seems file is attached, but when i click on send on gmail app it says:

Unfortunately, Gmail has stopped.

Please help me what is the problem and how can i fix it?

AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

MyActivity.java:

public class MyActivity extends Activity {

private static final int SELECT_PICTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 2;
private String selectedImagePath;
TextView uritv=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    uritv = (TextView) findViewById(R.id.uritxt);

    Button send = (Button) findViewById(R.id.sendBtn);
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (uritv.getText().toString().equals("URI")) {
                Toast.makeText(getApplicationContext(),"Please choose an image first!",Toast.LENGTH_SHORT).show();
            } else {
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("image/*");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"myemail@myemail.com"});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App");
                emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uritv.getText().toString()));
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            }
        }
    });

    Button galbtn = (Button) findViewById(R.id.galBtn);
    galbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
        }
    });

    Button cambtn = (Button) findViewById(R.id.camBtn);
    cambtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });

}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            uritv.setText(selectedImagePath.toString());
        }
    }
}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
}

解决方案

yes the problem is with the URI you build to get the file path. you are taking the raw file path which is not the case with the URI. it should be like below

content://media/external/images/media

Please check your path and test that again

这篇关于无法通过电子邮件发送的意图图片附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆