0

I want to call the sendPicture() from another class. but it seems i can't make sendPicture() a static type because it has startActivity() in it. i was wondering if its possible to call it from another class if so, how?

sendPicture() code:

 public static void sendPicture()
{

  File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
  Intent sendIntent = new Intent(Intent.ACTION_SEND); 
  sendIntent.putExtra("address", "number");
  sendIntent.putExtra("sms_body", "See attached picture"); 
  sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
  sendIntent.setType("image/jpg");  
  startActivity(sendIntent);
}
learner
  • 3,092
  • 2
  • 21
  • 33
michelle13
  • 89
  • 1
  • 11

2 Answers2

1

startActivity() is a method of Context so you need to have a reference to the context. You might try getApplicationContext().startActivity(sendIntent), or pass the Context into the static method and use that reference.

David C Adams
  • 1,953
  • 12
  • 12
  • @DavidCAdms thanks for answering. however i still dont understand how to do it. do you have any references for me to be able to get that? – michelle13 Feb 06 '14 at 00:19
  • First off you should understand how an Activity works, and what a Context is. Without that, you are going to be very very lost in the Android Framework. Start here: http://stackoverflow.com/questions/3572463/what-is-context-in-android – David C Adams Feb 06 '14 at 03:00
  • what if my method is not static? how can i call the sendPicture() method from another activity? – michelle13 Feb 06 '14 at 05:31
  • From an Activity, you need to pass it the Activity's Context. OR you can just use getApplicationContext().startActivity(sendIntent) in the code you already have. – David C Adams Feb 06 '14 at 06:05
0

I dont know whether it is correct or not, give it a try.Suppose you are having this so called function called 'sendpicture()' in class1 and u want to call it in class2. Then in class2 write the following code and try.

Class1  cls1= new Class1();
cls1.sendpicture();
Krishna Chandran
  • 389
  • 3
  • 18
  • Pretty bad form to call a static method from an instance. Also, since the method is static, he is going to get an error calling startActivity(). – David C Adams Feb 06 '14 at 03:04
  • @DavidCAdams:What if we import the Class1 in Class2 as follows : "import package_name.Class1;" . Then we can call the static function directly right? Btw i am not coming to argue i am just a beginner.Just expressed my idea that's it. – Krishna Chandran Feb 06 '14 at 06:28