how to select attachment in android: similar to email - android

I have an app. It's not an email app. But it has a feature similar to email, where a user may select to send an attachment along with a message. When I click to add attachment on my gmail, for example, I get the option to attach anything such as video, music, picture, file, etc. Has anyone ever build such an intent and unmarshalled the data and don't mind sharing their expertise?
edit
I am not able to get the multiple mime types in. I need the intent to be able to get video or image or music:
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_PICK);
attachment.setType("image/*,video/*,audio/*");
startActivityForResult(attachment, ATTACHMENT_REQUEST_CODE);
}
Right now it only launches the Photos or Gallery app. It does not load for audio, whereas the Gmail app attachment button does.
I think I got it, but I can't say 100% yet so I will reward #CommonsWare after testing
public void onGetAttachmentClicked(View view) {
Intent attachment = new Intent(Intent.ACTION_GET_CONTENT);
attachment.setType("*/*");
startActivityForResult(Intent.createChooser(attachment, "Pick Attachment"),
ATTACHMENT_REQUEST_CODE);
}

If you are trying to get content based on MIME type, use an ACTION_GET_CONTENT Intent with startActivityForResult(). The Uri delivered to your onActivityResult() method will point to the piece of content that the user chose.

I think you should simulate a html form(by assembling http header, parameters), and post your attachment to your server likes picking a file then submit the form

Related

Android: Sort attachments sent by mail

This is a silly and not important subject, but nothing to lose on asking.
My app has a "Share App" feature in main menu which sends a promotion email with text and a set of app screenshots, and in the end it calls the following method:
public static void shareFile(ArrayList<Uri> uris, String fileType)
{
try{
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType(fileType);
String strSubject = TMLocale.getStringResourceByName("mainmenu_sharethisapp_subject");
share.putExtra(Intent.EXTRA_SUBJECT, strSubject);
share.putExtra(Intent.EXTRA_TEXT, TMLocale.getStringResourceByName("mainmenu_sharethisapp_recommendtext"));
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
activity.get().startActivity(Intent.createChooser(share, TMLocale.getStringResourceByName("mainmenu_sharethisapp_shareapptitle")));
}
catch(Exception ex){
Toast.makeText(activity.get(), String.valueOf(ex.getMessage()),Toast.LENGTH_LONG).show();
}
}
This is working great, but what you receive -for example by mail- is an unordered set of attachments, I mean, the attachments order does not correspond to the ArrayList<Uri> order.
I'd like the attachments to be ordered as the ArrayList is, so who receives the email can see how the app works following the attachments (from Gmail -for example- and without downloading them) in a "readable"/"understandable" way, like following the same work sequence a user would follow using the app.
I have made several tests just to confirm attachments always arrive not only unordered, but in a different -maybe random- order.
Is it possible to sort the attachments?
I'd like the attachments to be ordered as the ArrayList is
Then use exactly one attachment, where you attach a document that puts your images in the desired order.
Otherwise... your code is starting an ACTION_SEND_MULTIPLE activity. There are likely thousands of these in the Play Store and elsewhere. How any of them handle your extras is up to their developers, not you or me.

What's the proper way to share via Intent.createChooser

The problem is I have an article that want to share to other apps, and I want to let the user to choose which app to share to. What I want to share is basically:
the title of the article
the URL of the article
the article content as HTML
the URL with some extra text (such as 'http://foo.com/article share from #FooApp')
All of these fields are optional, but I want to share at least one of them.
Such as when share via SMS or twitter, I want to set the content to part 4. when share via Facebook, I want to set 1, 2, 3 together. And when share via email, I want to set subject as 1 and message as 4.
I know (correct me if I'm wrong) every target intent receiver has it's own logic to pick up the fields it needed. So I want to provide as much information as possible and I wrote the following code:
String message = article.getURL() + " #FooApp";
Intent intent = new Intent().setData(Uri.parse(article.getURL())
.putExtra(Intent.EXTRA_SUBJECT, article.getTitle())
.putExtra(Intent.EXTRA_TEXT, message)
.putExtra(Intent.EXTRA_HTML_TEXT, article.getHTML())
.putExtra("sms_body", message)
...
.setType(HTTP.PLAIN_TEXT_TYPE);
startActivity(Intent.createChooser(intent, "Share to"));
But the problem is, it seems like a trick between setData, putExtra, setType.
For some apps appear in the chooser dialog, when I choose, the confirm share window (of that app) display nothing that I set to the intent. (for some other apps they just say failed to fetch resource)
For the putExtra part, when I add or remove some putExtra code, the target intent receivers diff a lot than I expected.
So the question is: am I doing it the wrong way? Are there some guideline for this problem?

How to send MMS without prompting to send it via gmail ,whatsapp or something else ?

When I am Using this code to send mms to specific user it shows me popup to send it via gmail,whatsapp,gtalk,message and etc. But in my case I just want to send that image as an mms to specific number that i will define in address field whithout showing any popup can any body tell me How to do this ? I googled for this and find lot of stuff on it.
Here is my code*strong text*
public void sendData(int num){
String fileString = "..."; //put the location of the file here
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.putExtra("sms_body", "text");
mmsIntent.putExtra("address", num);
mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileString)));
msIntent.setType("image/jpeg");
startActivity(Intent.createChooser(mmsIntent, "Send"));
}
Using an intent (like you did) is the preferred way because it's easy to implement and let the user choose his favorite app for the task of sending the MMS.
That being said you can still implement yourself the operation and send the MMS programmatically from your app by crafting and sending the appropriate HTTP request.
The following answer will provide you all the information you need: How to send image via MMS in Android?

Android: mime type for email attachment not set

I'm developing a very small application for Android 2.3.3.
I want to send an email (through the android email app) containing a jpeg image as an attachment, below the relevat code (tested only with sdk emulator):
public void sendArtwork(View aView){
EditText subj = (EditText)findViewById(R.id.edit_subj);
EditText descr = (EditText)findViewById(R.id.edit_descr);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/jpeg"); // attachment is a jpeg
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{"contribute#unintentional.org"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,subj.getText().toString()); //get subject from one EditText in the UI
emailIntent.putExtra(Intent.EXTRA_TEXT,descr.getText().toString()); //get body from one EditText in the UI
emailIntent.putExtra(Intent.EXTRA_STREAM, fileURI); // add attachment
startActivityForResult(Intent.createChooser(emailIntent, "Choose Email application:"), EMAIL_CODE);
}
It works as expected: it opens a Chooser, creates an email with the correct address, subject, text and attachment and sends it.
The only thing I'm not able to accomplish is to set the correct mime type for the image: the attachment is received correctly (i can detach it to disk and open it) but without a content type, so the email client (Thunderbird) does not display a preview and is not able to provide an application to open it.
Does anybody have advice on this?
----EDIT
The image file is sent across without any errors: as said, if I save it on disk on my PC and open it using a suitable application (i.e. Picasa) it shows up correctly.
I'm using the same method for sending emails, and have tested on various versions of a few email clients.
Even gmail is inconsitent, some versions sets the mime type of the attachment, others ignore it.
I've came to the conclusion that there is no safe solution. At least not by using an ACTION_SEND Intent.

Sending Sqlite database as email attachment in Android

I have a simple app which I am using to spike sending attachments.
I have managed to send a text file from the SD card (although I couldn't get this to work with a file created in the apps private area using openFileOutput(fileName, 0)) but I now want to send a database.
By debugging I can verify the database exists and has an entry in its only table. My code to send looks like this:
gmailButton = (Button) findViewById(R.id.button);
gmailButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "subject line");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Body of email");
Uri uri = Uri.fromFile(getDatabasePath("TEST_DB"));
//uri = file:///data/data/com.gmailspike/databases/TEST_DB
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("application/octet-stream");
startActivity(Intent.createChooser(sendIntent,"Email:"));
}
})
;
However, when the email client opens the attachment has a size of 0 bytes and if I touch to open the attachment the client says the file cannot be found.
Any ideas? I'm not sure the mime type is correct, or even if it is important!
I remember having had an issue like this too, when trying to send an email with an image attachment, linking to a file stored in the apps private data folder. The attachment is just missing.
Using the mail intent opens a new application to handle the mail-creation. Therefore you either need to write a Content Provider to allow other applications access to your private data.
Or copy the content to public area first and add it to the mail intent from there (this only works when a SD-Card is present with most phones). The External Storage.getExternalStorageDirectory() could maybe used in that case.
Hope this helps to find a solution.
As for the MIME type, it is surely important, since an application has to support it (explicitly or through wildcard) to respond to the intent and to be found via createChooser.
I think Gmail for instance accepts */* as MIME type with ACTION_SEND but I don't know about other mail clients.
Edit : And as for the permission, have a look at the FLAG_GRANT_READ_URI_PERMISSION flag from the Intent class : http://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION

Categories

Resources