upload kml map to my android app - android

i tried to find how i could load kml file and I found that I can use:
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri uri1 = Uri.parse("geo:0,0?q=http://code.google.com/apis/kml/
documentation/KML_Samples.kml");
mapIntent.setData(uri1);
startActivity(Intent.createChooser(mapIntent, "Sample"));
is there any other way such that i can upload the kml file locally from my pc rather than having to upload it to a website, then use it..as I am developing an applications and usually other users wont be able to access the kml of they dont have the username and password for the link
please if anyone can help, I would be thankful.

You can create an account in googlecode and upload the kml file, so it'll be on the internet and everybody can see it
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri uri = Uri.parse("geo:0,0?q=http://miruta.googlecode.com/files/miruta.kml");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
mapIntent.setData(uri);
startActivity(Intent.createChooser(mapIntent, "Sample Map"));
}

KML map in android is now officially available on this link.
You can easily load KML file from local resource folder or from inputstreams into your android app as below.
From local resource:-
KmlLayer layer = new KmlLayer(map, R.raw.geojson_file, context);
From InputStreams:-
InputStream inputStream = // InputStream containing KML data
KmlLayer layer = new KmlLayer(map, inputStream, context);

Related

How to open my app documents directory with Android File Manager

I've been trying to figure out a way to open the Android File Manager directly in my app's Documents directory so the user can select a JSON file among several without requiring the user to go search for the file path /storage/emulated/0/Android/data/com.company.app/files/Documents/. So far, I can make the "go find it yourself" tactic work, but not the "take the user to the directory for them" approach. Here's what I've tried:
// this is the "go find it yourself" approach that I've used:
String filename = this.getResources().getString(R.string.ExportImportFileNameString);
File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
Uri dirPathUri = Uri.fromFile(directory);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Intent.createChooser(intent, "Open in...");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, directory);
startActivityForResult(intent, IMPORT_REQUEST);
When my onActivityComplete handler is called for IMPORT_REQUEST I see the returned data looks like dat=content://com.lge.filemanager.FileProvider/storage/emulated/0/Android/data/com.company.app/files/Documents/SelectedFile.json flg=0x1 }
I've tried to invoke two different combinations of intent.setDataAndType instead of intent.setTypefollowing and that fails to let me select anything:
// This setDataAndType setup does not allow the user to open File Manager, nor navigate to the app Documents:
intent.setDataAndType(dirPath2, "application/json");
// This allows opening of File Manager but returns immediately without allowing the user to select a file, and returns a null data pointer:
intent.setDataAndType(dirPath2, "*/*");
Note that I've tried creating the intent object with ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT, and ACTION_VIEW with the similar result.
If I only have one file, I know I can have the app simply open a stream reader for a known file name as such:
File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File importFile = new File(directory, filename);
try
{
fis = new FileInputStream (importFile);
InputStreamReader inputStreamReader =
new InputStreamReader(fis, StandardCharsets.UTF_8);
...
However, that doesn't allow me the flexibility that I desire to allow a user to select from multiple files. Can anyone illuminate what's going on here and how to correct the situation.
Based on Commonsware feedback, here's what I have for the solution to this question:
//--------------
// Get the documents location for this app
File directory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File desiredFilePath = new File(directory.toString());
try{
// read all pathnames for files and directory
File[] allFilesInDirectory = desiredFilePath.listFiles();
// prepare array to place all path strings into
ArrayList<String> filesInDirectoryArray = new ArrayList<String>();
// retrieve each pathname file array
// but someone could simply use the "path" object instead
for(File path:allFilesInDirectory){
if (path != null){
// put all file paths into string array
filesInDirectoryArray.add(path.getPath());
// could discriminate based on file extension, if so desired
}
}
// send file path array for processing
ProcessDocuments(filesInDirectoryArray);
}catch(SecurityException e){
e.printStackTrace();
}

Open image from external public directory through intent

Recently I am developing a file sharing application and I created a GridView, where the downloaded files are being shown. From this View, I would like to be able to open the default application through an intent, to open the whole file. Currently I am testing the app with only image files. All the files are downloaded to the external public directory this way:
File externalFolder = new File(Environment.getExternalStorageDirectory(), "My application");
if(!externalFolder.exists()){
externalFolder.mkdir();
}
...
File folder = new File(externalFolder, "Images");
if(!folder.exists()){
folder.mkdir();
}
...
String filename = folder.getAbsolutePath() + "/" + fileToDownload.getName() + "." + fileToDownload.getExtension();
FileOutputStream fos = new FileOutputStream(filename);
When the file is downloaded, I scan it with MediaScannerConnection.scanFile. The scan is successful, the picture is visible among other files in Photos app.
After the file is downloaded, I am able to extract a thumbnail in the adapter of the GridView, so I surely have a valid path to the file.
And where the fun begins: I tried to set an onClickListener to the GridView items in the adapter to be able to open the picture in Photos app this way:
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if(new File(current.getPath()).exists()){
Log.e("path", "valid");
}else{
Log.e("path", "invalid");
}
Log.e("path", Uri.parse("content://"+current.getPath()).toString());
intent.setDataAndType(Uri.parse("content://"+current.getPath()), "image/*");
getContext().startActivity(intent);
}
});
The Intent is created successfully, I get the following in the log:
E/path: valid
E/path: content:///storage/emulated/0/My application/Images/best_picture_ever.jpeg
I have the option to choose among apps to open. When I select the app, it fails to open the image, like when it does not exist. All the 5 applications.
I tested this on my device with Oreo, and on two emulated devices with Nougat and Lollipop, all of them behaves the same way.
What am I doing wrong?
What am I doing wrong?
You are not creating a valid Uri. You cannot put content:// in front of arbitrary things and have a useful Uri, any more than you can put https:// in front of arbitrary things and have a usable URL.
Use FileProvider to serve up this file.

Unable to open online streaming PDF in browser

How to open a online stream assets in android.
I had used Intent, but the file gets downloaded and not getting opened in browser.
var uri = Android.Net.Uri.Parse(Url);
Intent intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
Don't want to use Web view. Any help ?
How to open a online stream assets in android.
You could using google doc service to achieve this function, it just need you
to modify your Url like this :
string Url = "https://docs.google.com/gview?embedded=true&url=" + strPdf;
//Eg :string Url = "https://docs.google.com/gview?embedded=true&url=" + "https://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
var uri = Android.Net.Uri.Parse(Url);

How to open pdf file in external app or in webview size is greater than 25mb

hello i want to show pdf file in webview or in external app.
1) i use this url
https://docs.google.com/gview?embedded=true&url= +pdfurl , but google says "Whoops There was a problen while previewing document"
2) if i use intent to open the url in external app then they didnot load the file in external app.
3) if i use the intent to open the url in browser then its start downloading the pdf file.
Intent ii=new Intent(Intent.ACTION_VIEW);
ii.setDataAndType(Uri.parse(pdf),"text/html");
try {
startActivity(ii);
}catch (ActivityNotFoundException e)
{
Toast.makeText(QuranPara.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
here is the complete url https://docs.google.com/gview?embedded=true&url=www.elibrary.alnoorpk.com/PDF%20Books/Lafzi_aur_bamahawrah_terjma_2015/Para%201%20(Lafzi%20aur%20Bamuhawra%20Tarjma).pdf
check the image
To show it in WebView try something like this
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
To show it in browser try something like this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
If your files are in assets first copy them to the external storage. Hope that you have handled permission flow.
Try the following code for offline
File pdfFile = new File(Environment.getExternalStorageDirectory(),"pdfName.pdf");//Pdf file path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
} else {
Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
}
Try the following for the webview option if you choose that
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "online pdf link";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
But Remember if you use google docs for viewing the pdf or any other doc, google docs may throw you the error stating
You've reached the bandwidth limit for viewing or downloading files that aren't in Google Docs format..... So doesn't seem reliable
So, be cautious with google doc viewing

How to display images stored in the Cache Folder using ACTION_VIEW

I'm creating an app that downloads a lot of pictures from a website and stores them all in the cache folder so that it won't take up too much space on the phone.
Now my problem is that I want the user to be able to click on an image and it will load up the image in the default android picture viewer. I've researched and figured out how to do that no problem, but I'm not 100% sure if this method will work. I can call the Intent no problem and the Pictureviewer opens but it doesn't display the images?
Can anyone let me know if this is possible to do this way? Thanks
Here is the code calling the intent and getting the directory and files...
URL url = null;
try
{
url = new URL(assetsToFullScreen[arg2]);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
File cacheDir = SingleArticle.this.getCacheDir();
String fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File file = new File(cacheDir, fileName);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file),"image/png");
startActivity(i);*/

Categories

Resources