选择图像并发送电子邮件?

时间:2013-02-21 08:48:21

标签: android android-layout android-intent

大家早上好,我是一个缺乏经验的开发人员,我需要你 我有这个代码来挑选图像并发送(附件)电子邮件... 请帮助我,因为这段代码失败了。

private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
private static final int REQ_CODE_PICK_IMAGE = 0;
Uri selectedImage;

public void galleria(View v){
     Intent in = new        Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
         startActivityForResult(in, RESULT_LOAD_IMAGE);  
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
           Uri selectedImage = imageReturnedIntent.getData();         
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
          emailIntent.setType("plain/text");
          emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"stefano.pietrella@gmail.com"});
          emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Object");
          emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Message...");

                            //Uri attachment = Uri.parse("file:///sdcard/fil.txt");
          emailIntent.putExtra(Intent.EXTRA_STREAM,selectedImage);
          startActivity(Intent.createChooser(emailIntent, "Send email..."));            
            }
        }
    }

logcat的

02-21 09:53:20.359: E/AndroidRuntime(16440): FATAL EXCEPTION: 
main 02-21 09:53:20.359: E/AndroidRuntime(16440): java.lang.IllegalStateException: 
Could not find a method k(View) in the activity class com.supermoney.campionatoruzzle.LeagueSummaryV2 for onClick handler on view class android.widget.Button with id 'buttonStartMatch2' 
02-21 09:53:20.359: E/AndroidRuntime(16440): at android.view.View$1.onClick(View.java:3678) 
02-21 09:53:20.359: E/AndroidRuntime(16440): at android.view.View.performClick(View.java:4211)

1 个答案:

答案 0 :(得分:0)

从日志cat中稍后放置注释似乎你已经在xml中放了一个onclick方法k并且在java counter部分没有声明

private static int RESULT_LOAD_IMAGE = 1;
    private static final int PICK_FROM_GALLERY = 2;
    private static final int REQ_CODE_PICK_IMAGE = 0;
    Uri selectedImage;

public void galleria(View v){
 Intent in = new        Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(in, RESULT_LOAD_IMAGE);  
}

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
       Uri selectedImage = Uri.parse("file://"+  getRealPathFromURI(imageReturnedIntent.getData());         
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("plain/text");
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"stefano.pietrella@gmail.com"});
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Object");
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Message...");

                        //Uri attachment = Uri.parse("file:///sdcard/fil.txt");
      emailIntent.putExtra(Intent.EXTRA_STREAM,selectedImage);
      startActivity(Intent.createChooser(emailIntent, "Send email..."));            
        }
    }
}

好像你从galery而不是路径中选择它会返回一个内容:// uri

         Uri selectedImage = Uri.parse("file://"+  getRealPathFromURI(imageReturnedIntent.getData())

尝试这个 - 如果来自galery,如果来自文件管理器去你以前的内容可以在if条件检查天气内完成数据包含mnt /然后它的真实路径去你的aproach否则使用此代码

   public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}