0
我只想發送一個圖像附件與gmail的意圖。以下代碼完美適用於所有4.0+設備,但在我的motorola razr 2.3上失敗。爲什麼???我如何讓它在4.0設備下工作?如何在android 2.3設備上附件發送電子郵件?
我有以下代碼:
public class SimpleEmailAttachmentActivity extends Activity {
private static final String sFileName = "myfile.png";
private Bitmap bitmap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_email_attachment);
final Button button = (Button) findViewById(R.id.email_snapshot_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View paramView) {
View rootView = button.getRootView();
if(rootView != null) {
rootView.setDrawingCacheEnabled(true);
Bitmap drawingCache = rootView.getDrawingCache();
bitmap = Bitmap.createBitmap(drawingCache);
rootView.setDrawingCacheEnabled(false);
}
saveToInternalStorage(bitmap);
File file = new File(getFilesDir() + File.separator + sFileName);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "email subject here");
intent.putExtra(Intent.EXTRA_TEXT, "email body here");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("plain/text");
startActivity(intent);
}
});
}
private void saveToInternalStorage(Bitmap bitmap){
FileOutputStream fos;
try {
fos = openFileOutput(sFileName, Context.MODE_WORLD_READABLE);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}