How do I open a specific folder with a file? - java

I'm trying to create an intent to open a folder with a file, but I still can't get access to it. When downloading, I save the file to the Downloads+"MyDirectory" folder, for example:
val destinationFolder: File = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS+"/MyDirectory//${FILE_NAME}")
request = DownloadManager.Request(url)
request.setAllowedOverRoaming(false)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"/MyDirectory//${FILE_NAME}")
.setDestinationUri(Uri.fromFile(destinationFolder))
And I'm trying to open it like this
var path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS+"/MyDirectory//")
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.setDataAndType(Uri.fromFile(path), DocumentsContract.Document.MIME_TYPE_DIR)
application.startActivity(intent)
And when I launch the application, a new folder is created, but the downloads folder opens, not my new folder. Do I want a new folder with my file to open when the intent is triggered, and not the downloads folder? How do I do this knowing the path to this folder?

Related

where to store file in android project

I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html

Can't get the correct music folder

I can't find out the correct folder to load my audio file.
I'm using an Android device which has no SD card but if I connect it via USB to my PC I will see the directory "Phone" with the subdirectory "Music".
In that directory I placed a mp3 file "Play.mp3" and then I tried the following variations to get a file reference:
File file = new File("/Phone/Music/Play.mp3");
File file = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), "Play.mp3");
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Play.mp3");
But in each case file.exists() returns false.

eclipse plugin load file

i have created a plugin which can load the image from the icons folder as follow:
Image image = ImageDescriptor.createFromURL(
FileLocator.find(bundle, new Path("icons/image.gif"), null))
.createImage();
It worked fine.
Similarly I have created a folder called "assets" inside eclipse plugin project, & created a file called "Temp.txt" inside it.
But I am unable to find a way to load it into my java class. Kindly help me out in getting through this.
Use:
URL url = FileLocator.find(bundle, new Path("assets/Temp.txt"), null);
URL fileUrl = FileLocator.toFileURL(url);
File file = new File(fileUrl.getPath());
... read the file in the usual way

I want to supply some pdf documents with my application

After certain research and lookup over the internet I found that I cannot place these files in the assets folders as those files will be private to my application, I want the native pdf application to be invoked on a button press which redirects the user to the particular document. What I am confused about is how do I bundle the files with my application so that when the user downloads or installs my application these files get stored in her SD-Card.
copy your assests files to the sdcard and when clicking on the button open the files in the sdcard as like this
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/filepath/"+fileName);//give the file path here
if(file.exists())
{
// do action here
}

How to prompt the "open with" menu after download a zip file

In my app I download .zip files. Is there a way when those files are downloaded, have a button/notification when clicked prompt the "open with" menu with third party compatible apps?
How to do that without knowing which zip handling apps are installed in the user phone?
Also, how to apply to apk files , to prompt app install ?
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setDataAndType(Uri.fromFile(file), "application/zip");
startActivity(intent);
If you add a button to open the file, it will take care of all of this automatically.

Categories

Resources