我正在构建一个 Android 应用程序,我想通过它立即邮寄从设备相机拍摄的照片。我希望这在后台完成,所以我使用了 JAVA Mail API,并且我正在启动一个服务来邮寄图像。
到目前为止,我的代码在图像 < 50 KB 的情况下工作正常,但是当我尝试邮寄图像 >= 500 KB 时它会抛出奇怪的错误。现在我正在为文件和用户名、密码使用硬编码值。
我的后台服务代码:-
public class BgMailer extends Service{
private ArrayList<String>Recievers ;
private String msg,sub;
public void onCreate() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service created",5000).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(this, DataHolder., duration)
//sendMail();
Notification note = new Notification( 0, null, System.currentTimeMillis() );
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 42, note );
Recievers = intent.getStringArrayListExtra("recievers");
sub = intent.getStringExtra("sub");
msg = intent.getStringExtra("msg");
sendMail(Recievers, sub, msg);
// stopForeground(true);
this.stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service destroyed",5000).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void sendMail(ArrayList<String>recvr,String sub,String msg){
Mail m = new Mail("r4jiv007@gmail.com", "<password>");
String [] address = new String[recvr.size()];
address = recvr.toArray(address);
m.set_to(address);
m.set_from("r4jiv007@gmail.com");
m.set_subject(sub);
m.setBody(msg);
try {
m.addAttachment("/sdcard/1.jpg");
//m.addAttachment("/sdcard/1.txt");
// m.addAttachment("/sdcard/bday.jpg");
if(m.send()) {
Toast.makeText(getApplicationContext(), "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
Log.e("MailApp", "Could not send email", e);
}
}
}
LogCat 中的错误值:-
10-20 01:34:13.690: W/ActivityManager(134): Timeout executing service:
ServiceRecord{40ae01d0 my.com.cnm/.BgMailer}
10-20 01:34:13.800: I/Process(134): Sending signal. PID: 1845 SIG: 3
10-20 01:34:13.800: I/dalvikvm(1845): threadid=4: reacting to signal 3
10-20 01:34:13.820: I/dalvikvm(1845): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.830: I/Process(134): Sending signal. PID: 134 SIG: 3
10-20 01:34:13.830: I/dalvikvm(134): threadid=4: reacting to signal 3
10-20 01:34:13.870: I/dalvikvm(134): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.870: I/Process(134): Sending signal. PID: 235 SIG: 3
10-20 01:34:13.870: I/dalvikvm(235): threadid=4: reacting to signal 3
10-20 01:34:13.880: I/dalvikvm(235): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.880: I/Process(134): Sending signal. PID: 239 SIG: 3
10-20 01:34:13.880: I/dalvikvm(239): threadid=4: reacting to signal 3
10-20 01:34:13.900: I/dalvikvm(239): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.900: I/Process(134): Sending signal. PID: 208 SIG: 3
10-20 01:34:13.900: I/dalvikvm(208): threadid=4: reacting to signal 3
10-20 01:34:13.900: I/dalvikvm(208): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:14.481: D/dalvikvm(134): GC_CONCURRENT freed 1427K, 32% free 9016K/13191K, external 607K/1119K, paused 8ms+16ms
10-20 01:34:14.821: D/dalvikvm(134): GC_EXPLICIT freed 379K, 32% free 9072K/13191K, external 607K/1119K, paused 171ms
10-20 01:34:15.432: I/Process(134): Sending signal. PID: 1657 SIG: 3
10-20 01:34:15.432: I/dalvikvm(1657): threadid=4: reacting to signal 3
10-20 01:34:15.432: I/dalvikvm(1657): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:15.442: E/ActivityManager(134): ANR in my.com.cnm:Bgmailer time=4035592
10-20 01:34:15.442: E/ActivityManager(134): Reason: Executing service my.com.cnm/.BgMailer
10-20 01:34:15.442: E/ActivityManager(134): Load: 3.78 / 3.97 / 3.79
10-20 01:34:15.442: E/ActivityManager(134): CPU usage from 13492ms to 0ms ago with 99% awake:
10-20 01:34:15.442: E/ActivityManager(134): 1.4% 230/com.htc.android.htcime: 1.3% user + 0% kernel / faults: 746 minor 3 major
10-20 01:34:15.442: E/ActivityManager(134): 4% 1845/my.com.cnm:Bgmailer: 3.7% user + 0.3% kernel / faults: 63 minor
10-20 01:34:15.442: E/ActivityManager(134): 2.2% 239/android.process.acore: 2.1% user + 0% kernel / faults: 12 minor
10-20 01:34:15.442: E/ActivityManager(134): 1.5% 272/dhd_dpc: 0% user + 1.5% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.2% 134/system_server: 0.7% user + 0.5% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.8% 81/akmd: 0% user + 0.8% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.6% 1657/my.com.cnm: 0.3% user + 0.2% kernel / faults: 4 minor
10-20 01:34:15.442: E/ActivityManager(134): 0.5% 424/com.android.htccontacts: 0.5% user + 0% kernel / faults: 54 minor
10-20 01:34:15.442: E/ActivityManager(134): 0.1% 208/com.android.systemui: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 271/dhd_watchdog: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 5/events/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 14/kondemand/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 64/kgsl-3d0/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 83/adbd: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 291/wpa_supplicant: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 552/com.ebuddy.android.xms: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 753/com.google.android.apps.maps:NetworkLocationService: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.1% 848/com.google.android.apps.maps:LocationFriendService: 0% user + 0% kernel + 0% iowait
10-20 01:34:15.442: E/ActivityManager(134): 19% TOTAL: 16% user + 2.3% kernel + 0.3% iowait + 0.1% softirq
10-20 01:34:15.442: E/ActivityManager(134): CPU usage from 1133ms to 1688ms later:
10-20 01:34:15.442: E/ActivityManager(134): 8.9% 134/system_server: 0% user + 8.9% kernel
10-20 01:34:15.442: E/ActivityManager(134): 5.3% 157/ActivityManager: 0% user + 5.3% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.7% 140/SurfaceFlinger: 1.7% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.7% 81/akmd: 1.7% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 3.5% 1722/akmd: 3.5% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.2% 1657/my.com.cnm: 1.2% user + 0% kernel / faults: 2 minor
10-20 01:34:15.442: E/ActivityManager(134): 14% TOTAL: 5.3% user + 8.9% kernel
10-20 01:34:15.452: I/Process(134): Sending signal. PID: 1845 SIG: 9
10-20 01:34:15.452: I/ActivityManager(134): Skipping background ANR: my.com.cnm:Bgmailer time=4035592
10-20 01:34:15.462: I/ActivityManager(134): Process my.com.cnm:Bgmailer (pid 1845) has died.
这里有什么与优先级或服务相关的吗,我什至尝试将我的服务作为前台应用程序运行,但没有用..而且我无法弄清楚如何解决这个问题。我将不胜感激任何帮助!
谢谢 :)
很好,感谢您针对此问题提出的建议,我刚刚创建了一个线程并通过它邮寄了图像,并且成功了..但是我想在邮寄过程成功完成时举杯祝酒,但出现以下错误:-
E/MailApp(2131): Could not send email
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast.<init>(Toast.java:68)
at android.widget.Toast.makeText(Toast.java:231)
at my.com.cnm.BgMailer.sendMail(BgMailer.java:86)
at my.com.cnm.BgMailer$1.run(BgMailer.java:47)
at java.lang.Thread.run(Thread.java:1027)