2015-01-08 2 views
0

로컬 파일을 첨부 파일로 추가하는 코드가 포함 된 Android 앱에 전자 메일을 추가했습니다.전자 메일을 사용하여 첨부 파일 추가하기 Android

case R.id.emailBtn: : 나는 의도를 열고 버튼을 "이메일 데이터"를 클릭하면

는하지만이 앱 충돌 을 얻고 고양이를 기록하면이 라인에서, http://hastebin.com/idejavunam.avrasm 다음, 널 포인터 예외의 오류는 출력을 보여줍니다

그래서 파일의 URI와의 문제를 생각하지만, 파일이 장치의 파일 시스템에 존재하는 이유를 볼 수 없습니다.

는 아무도 내가이 문제를 디버깅하는 방법을 알고 있나요? 아마도 파일의 경로를 전자 메일 의도에 잘못 전달하고 있습니까?

내가 솔루션을 구현하기 위해 다음과 같은거야 과정입니다.

 String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String fileName = "AnalysisData.csv"; 
     //this filePath is used in email code and converted to Uri. 
     filePath = baseDir + File.separator + fileName; 
     File f = new File(filePath); 

그리고이 이메일의 의도가 호출되는 코드는, 첨부 prposes에 대한 열린 우리당으로 변환 된 파일 경로 :

case R.id.emailBtn: { 
      Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show(); 
      Uri.fromFile(new File(filePath)); 
      Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
         "mailto","[email protected]", null)); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT"); 
      emailIntent.putExtra(Intent.EXTRA_STREAM, filePath); 
      startActivity(Intent.createChooser(emailIntent, "Send email...")); 


      break; 

답변

1
csv 파일을 생성하는 방법에서

코드

지금 작동하면 일부 부품 점검을 수정했습니다.

case R.id.emailBtn: { 
      Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show(); 
      Uri uri = Uri.fromFile(new File(filePath)); 
      Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
         "mailto","[email protected]", null)); 
      emailIntent.setType("*/*"); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT"); 
      emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
      startActivity(Intent.createChooser(emailIntent, "Send email...")); 


      break; 

UPDATE

는 또한 로그 캣보고 후 나는 당신의 파일 경로가 null 인 것으로 나타났다. 친절하게 맞

편집

난 당신의 onclick 방법은 단순히 또한, 작동 경우

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); 
    String fileName = "AnalysisData.csv"; 
    filePath = baseDir + File.separator + fileName; 
    File f = new File(filePath); 
    switch (v.getId()) { 
     case R.id.exportBtn: { 
      Toast.makeText(this, "select clicked", Toast.LENGTH_SHORT).show(); 
      //write sample data to csv file using open csv lib. 
      date = new Date(); 



      CSVWriter writer = null; 

      // File exist 
      if(f.exists() && !f.isDirectory()){ 
       FileWriter mFileWriter = null; 
       try { 
        mFileWriter = new FileWriter(filePath , true); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       writer = new CSVWriter(mFileWriter); 
      } 
      else { 
       try { 
        writer = new CSVWriter(new FileWriter(filePath)); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      String data [] = new String[] {"Record Number","Ship Name","Scientist Name","Scientist Email","Sample Volume","Sample Colour","Sample Material","Latitude","Longitude","Date","\r\n"}; 
      writer.writeNext(data); 

     /* 
     //retrieve record cntr from prefs 
     SharedPreferences settings = getSharedPreferences("RECORD_PREF", 0); 
     recordCntr = settings.getInt("RECORD_COUNT", 0); //0 is the default value 
     */ 

      //increment record count 
      recordCntr++; 

     /* 
     //save record cntr from prefs 
     settings = getSharedPreferences("RECORD_PREF", 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putInt("RECORD_COUNT",recordCntr); 
     editor.commit(); 
     */ 
      data = new String[]{Integer.toString(recordCntr),shipName,analystName,analystEmail,sampleVolume, 
        sampleColour,sampleMaterial,latitudeValue.toString(),longitudeValue.toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),"\r\n"}; 

      writer.writeNext(data); 
      try { 
       writer.close(); 
       Toast.makeText(this, "Data exported succesfully!", Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Toast.makeText(this, "Error exporting data!", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
     } 

     case R.id.emailBtn: { 

      Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show(); 
      if (f.exists() && !f.isDirectory()) { 
       Uri uri = Uri.fromFile(f); 
       Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
         "mailto","[email protected]", null)); 
       emailIntent.setType("*/*"); 
       emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT"); 
       emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
       startActivity(Intent.createChooser(emailIntent, "Send email...")); 
      } 


      break; 
     } 
    } 

} 
+0

친절 업데이트를 찾을 수에 대한 오류의 위치를 ​​말해 교체에 맞게 코드를 수정 수정 한 제공된 코드 –

+0

위의 코드 조각에서 흐름을 얻는 것이 어렵 기 때문에 @BrianJ는 완전한 Java 파일을 공유 할 수 있습니까? –

+1

내 대답을 업데이트했습니다. –

관련 문제