我必须在从相机拍照后通过电子邮件发送图像。当我打印所需图像的地址时,它打印正确,存储在filePath
变量中;它显示了/mnt/sdcard/DCIM/camera/2013-04-2921.33.45.jpg,但是当我发送它时,它会在应用程序中显示附件,但在发送邮件后,接收方不会收到附件。
我的应用程序的基本结构是:
以下是附件的代码:
public class INF extends Activity {
String pic,fullPath;
private static final String TAG = "MyActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.in);
final EditText et = (EditText)findViewById(R.id.et1);
final EditText et2 = (EditText)findViewById(R.id.et2);
Button bt = (Button)findViewById(R.id.bt);
getLastImageId();
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"pravind.india@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "My Information");
i.putExtra(Intent.EXTRA_TEXT, "Name:"+et.getText().toString()+'\n'+"Mobile No."+et2.getText().toString());
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(fullPath));
startActivity(Intent.createChooser(i, "send email...."));
}
});
}
private int getLastImageId(){
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.d(TAG, "getLastImageId::id " + id);
Log.d(TAG, "getLastImageId::path " + fullPath);
imageCursor.close();
return id;
}else{
return 0;
}
}
}
getLastImageId
存储相机在变量fullPath
中拍摄的最后一张图像的路径。
答案 0 :(得分:1)
您的错误是您选择了text
而不是octet-stream
的意图类型
你必须这样做
setType("application/octet-stream")
而不是
setType("plain/text");
答案 1 :(得分:0)
添加对媒体商店的权限访问权限,请尝试以下操作:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body", "some text");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
sendIntent.setType("image/png"); //set content type
检查uri正确工作或不看:
Uri uri = Uri.parse("content://media/external/images/media/1");
或
Uri uri = Uri.parse("file://mnt/sdcard/test.jpg");
或
Uri uri = Uri.parse("file://"+Environment.getExternalStorageDirectory()+"/test.jpg");
示例:How to send an MMS Programmatically in Android
如果没有帮助,请通过java mail transport发送您的数据,请参阅: