我已经想出了如何访问默认图库,我已经想出了如何使用电子邮件功能进行文字,但我想在我的图库中通过电子邮件发送图片,我该怎么做?我打算尝试使用下面的代码但是错误不断出现“getTempUri”,我忽略了什么,我感谢任何帮助。感谢
public void onClick(View v) {
Intent photosendbutton = new Intent(Intent.ACTION_SEND);
photosendbutton.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
photosendbutton.putExtra(Intent.EXTRA_EMAIL, new String[]{"johnnywalker@hotmail.com"});
photosendbutton.setType("image/*");
Uri parse = getTempUri("/mnt/sdcard/yourfolder");
photosendbutton.putExtra(MediaStore.EXTRA_OUTPUT, parse);
startActivity(Intent.createChooser(photosendbutton, "Send mail..."));
答案 0 :(得分:0)
这是因为您尝试从onclicklistener中访问方法。方法getTempUri是在onClickListener之外定义的,但你可能看不到它,因为onClickListener是内联声明的:
class YourContainingClass{
public Uri getTempUri(String){
//process string
}
public void someOtherMethod(){
button.setOnClickListener(new OnCLickListener(){
public void onClick(View v) {
//this is where you're trying to access getTempUri,
//from within the new OnClickListener instance
}
});
}
}
如果你的包含类是Activity:
,你可以通过这样访问getTempUri来解决这个问题((YourContainingClass)v.getContext()).getTempUri()
否则你可以让你的包含类实现onClickListener,如下所示:
class YourContainingClass implements OnClickListener
{
public void someMethod(){
button.setOnClickListener(this);
}
public void OnClick(View v){
//put your code here, here you have access to getTepUri
this.getTempUri();
}
public Uri getTempUri(String someString){
//your logic
}
}
我意识到这可能很难遵循,但尝试一下并随意提问