Simple way to read data from html file with my app? - android

I'm developing an app that should be able to transfer simple data between devices.
So first step is to send some data from my app on Device 1 to Device 2. I'm using the code below:
Button btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Name:"+oldName+",Surname:"+oldName);
startActivity(sendIntent);
}
});
After the button click Android share menu appears and I select Bluetooth option. I use it to send data to Device 2 and it gets there as file with extension ".html".
Now I would like to open that file and use data stored inside in my app on second device.
I click on file in my Bluetooth folder and I choose my app from menu with suggested apps to use with html files.
My app on second device starts but I can't get data from the file.
What is the most simple way to get data with my app from that file?
Should I use ACTION_VIEW?

In the activity that is opened you need to get the data via the Intent object
Intent intent = getIntent();
Uri uri = intent.getData();
String filename = null;
if (uri!=null)
filename = uri.getPath();

Related

Android open/view pdf: intent chooser cannot find any application for opening pdf file stored in 'Google Drive'

I was trying to create a chooser to grant users freedom to open a pdf file with the application they prefer.
Here's my approach:
private void openPdf(String pdfUrl, String mime) {
// Intent pdfViewIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Intent pdfViewIntent = new Intent(Intent.ACTION_VIEW);
pdfViewIntent.setDataAndType(Uri.parse(pdfUrl), mime);
Log.d(TAG, "openPdf: uri: " + Uri.parse((pdfUrl)));
Intent chooser = Intent.createChooser(pdfViewIntent, "Choose an app");
// chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // optional
startActivity(chooser);
finish();
}
The pdf url is something like this: https://drive.google.com/file/d/.../view?usp=sharing
And for the mime type, I tried using both "application/vnd.google-apps.document" and "application/pdf".
Now, when the user clicks on document, I get an output like the following:
But I want an output like the following screenshot: (it appears when I try to open the same link with WhatsApp)
NOTE 1: using WebView is not a good option for me.
NOTE 2: the above approach works if the URL is in the format https://example.com/.../filename.pdf. But it fails for Google Drive's link.

Android: Displaying a PDF inside android application

In the application I receive a PDF from an API call so it is inside a byte array and I would like to display it inside the application without having to save it in the user's phone. I have tried a WebView but it has not worked. Seems like a WebView will display a PDF from an url but will not render it if you give it the PDF as a string.
I was wondering if there was a way to display a PDF inside an android application without having to save it in the user's phone?
I am doing this by writing the received bytes of the pdf file to a temporary cache DIR and then open it with an intent. So for example in an async task I download the file in the doInBackground method and do this in the onPostExecute.
#Override
protected void onPostExecute(byte[] result) {
final File reportFile = new File(context.getExternalCacheDir(), "pdf-file.pdf");
final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(reportFile));
output.write(result);
output.close();
Uri path = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".fileprovider", reportFile);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
}
This is working quite well. The only thing you have to do is to check if the user has a PDF reader installed that is working.
The advantage I see with this solution is that you provide the user the opportunity to further do with the pdf what he or she wants (e.g. print, store, share, ...). If you just display it within a frame in your app you would quite limit the interaction possibilities (or would have to implement them all by your self).

how to open other file manager apps via intent?

in my app i use the code below to open up the downloads and only see PDF files. But it opens up the default Android file manager and for other parts of my app to work i need to use other file managers (i am using ES File Manager) to open up instead.
How do i open up other file manager apps?
(giving the user an option to choose from multiple file manager apps helps too)
My code as of now:
public void PDF() {
PDF = (Button) findViewById(R.id.FindPDFBtn);//Finds the button in design and put it into a button variable.
PDF.setOnClickListener(//Listens for a button click.
new View.OnClickListener() {//Creates a new click listener.
#Override
public void onClick(View v) {//does what ever code is in here when the button is clicked
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select a PDF "), SELECT_PDF);
}
}
);
}
The problem with third party file explorers is that most of them don't support the same kind of intents (as they are not standardized).
For ES File explorer, check this: http://www.estrongs.com/res/develop_en.htm

Trying to attach image to mms/sms after select attachment from MMS app

I'm an trying to get the image to attach to mms after you pick my app form the attachment picklist. The image code is fine.
What I want to happen
1.You are in a text message, you click the attach button
2.you select images, it pulls up the chooser of apps
3.select my application, has gridview of images
4.(The Issue) - you select the photo you want from my app and it sends it back into the mms you where in
I'm not sure how to respond though to the ACTION_GET_CONTENT from the sms/mms app so that my app sends the image back to it.
Uri uri = Uri.fromFile(file);
Intent localIntent = new Intent(android.content.Intent.ACTION_SEND);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
//localIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
localIntent.putExtra("android.intent.extra.STREAM", uri);
localIntent.setType("image/jpeg");
startActivity(localIntent);
After literally a week of looking around I found it.
Intent localIntent = new Intent(); localIntent.setData(imageURI(position));
setResult(Activity.RESULT_OK, localIntent);
dialog.dismiss();
finish();
I needed to use setResult

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