Android App to Send Images Via Email - android

Ok once again, I am in way over my head here while learning android. After finally developing my simple little app, I am trying to use some of the benefits of having a native app.
So, project one, make a page which can send images via email (either from the gallery or camera)
Essentially its a select and send via email, but I don't even know where to start.
I found some code that somebody else was asking about at;
Android App Take/ Email Photo
I tried this, but get all sorts of errors from eclipse, replating to downloadedPic section.
If somebody could please take a look and advise me on the best way to do this, that would be amazing. As usual sorry for my beginers stupidity, but I guess everyone has to learn somewhere
THIS IS MY .JAVA AT PRESENT
public class Photos extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_photos, menu);
return true;
}
THIS IS MY .XML AT PRESENT
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please chose the options below to select and upload photos into the
DCC for the selected project..."
tools:context=".Photos"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

First what should you do is get the image storage path by using the file,
File *photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png");
Then Convert that file path in to Uri
Uri imageuri = Uri.fromFile(photo);
Finally send it the image via email using your imageuri
Intent send_report = new Intent(Intent.ACTION_SEND);
send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid});
send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
send_report.putExtra(Intent.EXTRA_TEXT, email_body);
send_report.setType("text/plain");
send_report.setType("image/png");
startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);
Hope it helps.

Get Your Image First :
// Get Image form mnt/sdcard/YOUR_FLODER/my_image.png
ImageView my_Image = (ImageView)findViewById(R.id.my_Image);
Imagepath="/sdcard/YOUR_FLODER/"+my_iamge+".png";
bitmap = BitmapFactory.decodeFile(Imagepath);
Fetch mail address:
// This Will fetch merchant's Email id from Deivce.
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
AccountManager manager =(AccountManager)getSystemService(ACCOUNT_SERVICE);
//Account[] accounts = AccountManager.get(getApplicationContext()).getAccounts();
Account[] accounts = manager.getAccounts();
for (Account account : accounts)
{
if (emailPattern.matcher(account.name).matches())
{
possibleEmail = account.name;
}
}
Send Click Event :
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("image/png");
i.putExtra(Intent.EXTRA_CC,new String[]{possibleEmail});
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail With Image attachment");
startActivity(Intent.createChooser(i2, "Send Email..."));
At The End Photos.java
public class Photos extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity_Photos);
// your image fetching code
// fetch mail code
// write button click event
// put intent code in click event
}
}
So Hope Now you Get Full Code.

Related

Send data from unity app to android app on button press

I have two apps one is android java app and other is unity app
Unity app recognize the object and shows some details. What i want to do is if user clicks the button which is shown when the object is identified, i want to send that information to the java android app so that the app can use that data to perform certain functions. How can i send the data and switch from unity to java android app after user clicks the button?
EDIT: For any one looking for the answer. I manage to do it by the code below
IN UNITY APP:
public void LaunchAppMessage()
{
string bundleId = "com.example.sidenavtest";
bool fail = false;
string message = "message";
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
try
{
launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
launchIntent.Call<AndroidJavaObject>("putExtra", "arguments", message);
}
catch (System.Exception e)
{
fail = true;
}
if (fail)
{
Application.OpenURL("https://google.com");
}
else
{
ca.Call("startActivity", launchIntent);
}
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
}
IN ANDROID APP
#Override
protected void onStart() {
super.onStart();
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("arguments");
}
}
If i understand you correctly you want to have both applications on the same device.
Simplest way would be to save the data ino a file in the unity application and read from that file in the android application. But i think you can even attach data on application calls so that you dont even need to save it temporarily.
Opening the Android app from Unity is possible aswell. Here someone asked the same question and got some working answer.
Edit: From what i can gather you can transfer your data via intents, it seem's like most people work with JAR-Plugin from where you can make a intent call.

How to check if intent (activity) contains string in it's package name (Espresso)?

I test my app with UI testing and would like to check if the camera app opens.
I have did this with:
#Test
public void profileImageClickOpensCamera() {
mIntentsRule.getActivity().startActivity(new Intent(mIntentsRule.getActivity(), ProfileActivity.class));
onView(withId(R.id.circleProfileImage)).perform(click());
intended(toPackage("com.android.camera"));
}
It is working fine on most devices, however if I rain it on SAMSUNG Galaxy S8, which has "com.sec.android.app.camera" package of it's camera app, the test fails.
My question is, how could I check with espresso that the package contains the word "camera" ?
It's not the best solution because a device's camera app's package name could be anything, but even better then what I got know.
So I would like to do something like:
intended(StringContains(toPackage("com.android.camera")));
Any suggestions?
Thanks in advance.
You can test the intent action instead of package.
Something like intended(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)) or intended(hasAction(equalTo(MediaStore.ACTION_IMAGE_CAPTURE))) should work.
I had the same situation and I've managed to solve it like this:
PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager();
String pack = resultData.resolveActivity(packageManager).getPackageName();
intended(toPackage(pack));
In my situation I had an activity with a button which opens the camera, lets you take a picture and returns with it in your activity. The full code of this test would be:
#Test
public void testCameraIntent() {
Bitmap icon = BitmapFactory.decodeResource(
InstrumentationRegistry.getTargetContext().getResources(),
R.drawable.husky);
// Build a result to return from the Camera app
Intent resultData = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager();
String pack = resultData.resolveActivity(packageManager).getPackageName();
// Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
// with the ActivityResult we just created
intending(toPackage(pack)).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.btn_takePicture)).perform(click());
intended(toPackage(pack));
}
And this is the result :-) the image with Husky dog is a local image I've set to be sent in my custom ActivityResult:

How to share image using Branch-io by email in android

I am using Branch-io to share content like Text, image(from URL), and Link.
I am all done and working perfect with Facebook, Whatsapp. I mean I am able to share Text and Link on Gmail also but unable to attach image.
This problem occurred only with Gmail.
Here is my code:
BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
.setCanonicalIdentifier("item/12345")
.setTitle(title)
.setContentDescription(message)
.setContentImageUrl(image)
//.setContentImageUrl(Uri.parse("file://"+downloadedImagePath).toString())
.setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PUBLIC);
LinkProperties linkProperties = new LinkProperties()
.addControlParameter("$always_deeplink", "true")
.setFeature("sharing")
.setStage("1");
String body = "Text Message";
ShareSheetStyle shareSheetStyle = new ShareSheetStyle(mContext, mContext.getResources().getString(R.string.app_name), body)
.setAsFullWidthStyle(true)
.setSharingTitle("Share With");
branchUniversalObject.showShareSheet((Activity) mContext,
linkProperties,
shareSheetStyle,
new Branch.BranchLinkShareListener() {
#Override
public void onShareLinkDialogLaunched() {
LogUtils.v("Share Link", "Launched");
}
#Override
public void onShareLinkDialogDismissed() {
LogUtils.v("Share Link", "Dismissed");
}
#Override
public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchError error) {
LogUtils.v("Share Link", sharedLink);
}
#Override
public void onChannelSelected(String channelName) {
LogUtils.v("Share Link", channelName);
}
});
}
}, false);
Let you know again, this code working perfect with Facebook, whatsapp but not with Gmail(only issue not attaching image).
If anyone knows then please let me know what I'am doing wrong and what is the perfect way to do this.
Thanks in advance.
I just tested this on our test application, and everything works correctly when sharing with Gmail. Here's my test link: https://appsolutely.test-app.link/Mw2qFHRMxJ
Whenever you share a link via showShareSheet(), all the Branch Universal Object (BUO) information is attached to the link. If you keep sharing links for the same data, but across different channels, the only difference between the links for each channel will be "~channel": "Gmail".
Looking at your code, I do not see any issues you could encounter with attaching the image URL, as you do call .setContentImageUrl(image) for the BUO object, so the issue may be in the way you are testing. After sharing your link via Gmail, I would suggest pasting it into the URL bar of your browser and attaching the following query parameter: ?debug=true.
E.g.: https://appsolutely.test-app.link/Mw2qFHRMxJ?debug=true
You could then view all of the data the link contains on the page that opens. If you see that the data contains the "$og_image_url" with the correct URL address, it means that the image was actually attached, as shown in the example below for my test link https://appsolutely.test-app.link/Mw2qFHRMxJ

How to open a specific album or photo in facebook app using intent from your own Android App

How to open an album or photo in facebook app using intent from your own Android App?
I been searching for the specific answer for this one. Most question are about how to open a facebook page via intent.
I saw this one (regards to Sunny a Sr. Software engineer at iAppStreet) but it doesn't work.
public Intent getOpenFacebookIntent(String pId) {
try {
activity.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("facebook:/photos?album=0&photo=" + pId+"&user="+ownerId));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"));
}
}
startActivity(getOpenFacebookIntent(pid));
thanks.
Actually i made this question to help those who have the same problem like me. As of today (28 March 2016) i just found out how to open an album using intent but i cannot open a photo using intent in the facebook app. I am a beginner in android programming so please bear with me.
I used a button to implement this. Here is the code:
Button buttonOpenALbum = (Button) findViewById(R.id.button);
buttonOpenALbum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//It is a album from a facebook page # www.facebook.com/thinkingarmy
String albumID = "575146485903630";
String userID = "575145312570414";
String url = "facebook:/photos?album="+albumID+"&user="+userID;
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
If you want to get the albumID and userID of a facebook album try searching on google how or try this one if it helps, https://www.youtube.com/watch?v=sMEmlpmCHLc
Modifying the code to open a specific photo has not produce the expected result. Even though I included the photoID, you still end up in the album. Here is the code:
Button buttonOpenPhoto = (Button) findViewById(R.id.button);
buttonOpenPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//It is a album from a facebook page # www.facebook.com/thinkingarmy
String albumID = "575146485903630";
String userID = "575145312570414";
String photoID = "803154029769540";
String url = "facebook:/photos?album="+albumID+"&photo="+photoID+"&user="+userID;
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
I can't be sure why. I tried looking but some say the urls of facebook is undocumented. If I may, I think you cannot open a specific photo because upon observation, facebook open a photo in full screen, meaning it is a different Activity than the Main Activity which handles the incoming Intent. The Main Activity of the facebook app only handles the intent up to opening a specific album.
I hope I can help other "new android coders" with my first post.
NB: I just learn coding using google and stackoverflow. It's my way of giving back. Thanks.
Launch a view intent with the following URL
https://www.facebook.com/photo.php?fbid={photo_id}
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
ctx.startActivity(browserIntent);
If the app is installed, it will prompt the user to open in app, otherwise it will use the browser
Some useful information at Open Facebook Page in Facebook App (if installed) on Android
In particular, new versions of facebook app work with
Uri.parse("fb://facewebmodal/f?href=" + FACEBOOKURL
I found this the simplest approach to acheive what was needed.
Note also that to get the URL you can go to the facebook album (on facebook desktop), click the three dots next to "Edit" and "Tag" and select "Get Link". I found this more reliable than simply copying the URL from the browser.

Android: Share to Messenger.. unfortunately stopped

I tried to send id of an image from an activity to another activity. But unfortunately stopped. But i couldn't find any solution. This is my main activity. I kept all image id in a class Utils.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent=new Intent(MainActivity.this,SendActivity.class);
intent.putExtra("id",Utils.THUMBNAIL_IDS[position]);
startActivity(intent);
}
});
And this is my sendActivity from where i tried to share this image to messenger:
Intent intent = getIntent();
position=getIntent().getExtras().getInt("id");
Now the code to share to messenger:
private void onMessengerButtonClicked() {
// The URI can reference a file://, content://, or android.resource. Here we use
// android.resource for sample purposes.
Uri uri = Uri.parse("android.resource://com.example.amit.bengalistickerfun/drawable/" +
MainActivity.mAdapter.getItem(position));
// Create the parameters for what we want to send to Messenger.
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "image/jpeg")
.setMetaData("{ \"image\" : \"tree\" }")
.build();
// Sharing from an Activity
MessengerUtils.shareToMessenger(this, 0, shareToMessengerParams);
if (mPicking) {
// If we were launched from Messenger, we call MessengerUtils.finishShareToMessenger to return
// the content to Messenger.
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
// Otherwise, we were launched directly (for example, user clicked the launcher icon). We
// initiate the broadcast flow in Messenger. If Messenger is not installed or Messenger needs
// to be upgraded, this will direct the user to the play store.
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
Can anybody help me?
I would suggest to put try catch block here , so that you can catch the exception. if any
try to observe log-cat and view the errors
try using android inbuilt logging class to see the logs
Post your error logs here ,so that we can see the issue and provide our inputs accordingly.

Categories

Resources