List downloaded files in Android using Spinner - android

My app involves downloading a few csv files and then choosing one of them to perform some functions. After the user downloads the required files, a spinner must display the files that have been downloaded. On selecting the required file, it must link to another activity where the path of the file chosen is the FileName. Is this possible using a spinner and how do I go about it?
File selected = new File("/storage/emulated/0/Download/");
String item_ext = "";
try {
item_ext = selected.getName().substring(selected.getName().lastIndexOf("."));
} catch (StringIndexOutOfBoundsException e) {
item_ext = "";
}
if(item_ext.equalsIgnoreCase(".csv")) {
Intent txtIntent = new Intent();
txtIntent.setAction(android.content.Intent.ACTION_VIEW);
txtIntent.setDataAndType(Uri.fromFile(selected), "text/csv");
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
try {
startActivity(txtIntent);
} catch(ActivityNotFoundException e) {
txtIntent.setType("text/*");
startActivity(txtIntent);
}
}

Since my application requirement mainly dealt with downloaded files, I linked the app to Downloads folder. By clicking on the file of interest, the path for the file was obtained. This link helped to get the absolute path and was suitably modified for the purpose.

Related

How to delete a file using the path

I'm building an app that allows the user to save the bitmap or share it without saving it. The 2nd functionality doesn't quite work. I understand that the app needs to save the file to the device before sharing it on a social media app so my idea was, immediately after the file was successfully shared, to automatically delete the file from the device. I've build a delete method trying 2 different approaches and neither have worked:
First approach:
public void deleteFile(String path){
File file = new File(path);
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
}
Second approach:
public void deleteFile(String path){
File file = new File(path);
boolean deleted = file.delete();
}
And I'm calling deleteFile(String) from the sharing method:
public void shareMeme(Bitmap bitmap) {
String path = MediaStore.Images.Media.insertImage(Objects.requireNonNull(getContext()).getContentResolver(), bitmap, "Meme", null);
Uri uri = Uri.parse(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));
deleteFile(path);
}
With respect to your stated problem, insertImage() returns a string representation of a Uri. That Uri is not a file. Calling getPath() on it is pointless, and you cannot delete anything based on that path.
More broadly, if your intention is to delete the content right away:
Do not put it in the MediaStore
Do not share it, as you will be deleting it before the other app has a chance to do anything with it
If you want to share it, but then delete it:
Do not put it in the MediaStore
Delete it the next day, or in a few hours, or something, as you have no good way of knowing when the other app is done with the content
To share an image with another app without using the MediaStore:
Save the image to a file in getCacheDir() (call that on a Context, such as an Activity or Service)
Use FileProvider to make that file available to other apps
Beyond that:
Do not use wildcard MIME types in ACTION_SEND. You are the one who is supplying the content to send. You know the actual MIME type. Use it.
Note that there is no requirement for an ACTION_SEND activity to honor both EXTRA_TEXT and EXTRA_STREAM. Most seem to do so, but that behavior is outside of the ACTION_SEND specification.
Note that insertImage() is deprecated on Android Q.
First, you need to check if your file exists, (maybe you set the wrong path?). Then delete the file
File file = new File(path);
if (file.exists()){
if (file.delete()) {
Toast.makeText(this, "file Deleted :" + path, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "file not Deleted :" + path, Toast.LENGTH_SHORT).show();
}
}

how to share audio through share it from another app

I want to share audio file in different social apps from my app. I am using following code to share audios. it is working well for whatsapp but not working for shareit. shareit app opens but fails to retrieve the file and says "Sending this type of content is not supported". the file was of "mp3" format. I could share the file from file manager using the android generic share option and then selecting shareit.
public void shareAudio(String packageName, String platformName) {
checkAndPauseAudioPlayer();
try {
//Copy file to external ExternalStorage.
String mediaPath = audioFilePath;
Intent shareMedia = new Intent(Intent.ACTION_SEND);
shareMedia.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//set application package
shareMedia.setPackage(packageName);
shareMedia.setType("audio/*");
//set path of media file in ExternalStorage.
shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
startActivity(Intent.createChooser(shareMedia, platformName+" Is Not Installed!"));
} catch (Exception e) {
showSnackBar("Please Install "+platformName+" First!",Snackbar.LENGTH_LONG);
}
}
add permission in manifest file :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

how to hide picture from gallery but show in storage of system?

I have an application that download picture from internet.I want when download picture don't show in gallery but show in SD card and folder because the user can delete the file.
Like of telegram.
When i add . first of name of file hide from everywhere
Here is the code:
private void createNoMedia() {
final File nomedia = new File(getConversationsFileDirectory()+".nomedia");
if (!nomedia.exists()) {
try {
nomedia.createNewFile();
} catch (Exception e) {
Log.d(Config.LOGTAG, "could not create nomedia file");
}
}
}
public static String getConversationsFileDirectory() {
return Environment.getExternalStorageDirectory().getAbsolutePath()+"/YourFolder/";
}
Try this.
Download a image file using your application instead of using default download manager API.
Save a file with a different extension which is not jpg or png or any other image ext.
Only your app can understand the extension and display it as a image.

Android - Select file from internal storage folder

I have saved a bunch of videos in an internal storage folder. Afterwards, I want the user to be able to select one of these videos in this specific folder. I tried using ACTION_GET_CONTENT in an attempt to let another app do this for me, without any success, as it just opens up a file browser in some other directory.
What I have now is:
public static File getOwnVideosDirectory(Context context) {
String ownVideosDirPath =
context.getFilesDir().getAbsolutePath() + File.separator + "OwnVideos";
File ownVideosDir = new File(ownVideosDirPath);
if (!ownVideosDir.exists()) {
ownVideosDir.mkdirs();
}
return ownVideosDir;
}
private void dispatchExistingVideo() {
Log.d(LOG_TAG, "> dispatchExistingVideo");
Intent videoPicker = new Intent(Intent.ACTION_GET_CONTENT);
File ownVideosDir = Utility.getOwnVideosDirectory(getContext());
videoPicker.setDataAndType(Uri.fromFile(ownVideosDir), "video/*");
if (videoPicker.resolveActivity(getContext().getPackageManager()) != null) {
startActivityForResult(videoPicker, REQUEST_EXISTING_VIDEO);
}
}
So I'm wondering, am I doing something wrong or is it impossible like this. If impossible: is there any library,... available that would allow me to do what I want, or any direction on how I could implement this myself as a last resort?
Thanks in advance
Please take a look at that library - Material File Picker
It allows to show a dialog with the specified path using .withPath(Utility.getOwnVideosDirectory(getContext()).getAbsolutePath()).
The whole creation code:
new MaterialFilePicker()
.withActivity(this)
.withRequestCode(1)
.withFilter(Pattern.compile(".*\\.txt$")) // Filtering files and directories by file name using regexp
.withFilterDirectories(true) // Set directories filterable (false by default)
.withHiddenFiles(true) // Show hidden files and folders
.withPath(Utility.getOwnVideosDirectory(getContext()).getAbsolutePath())
.start();

Check whether data inside data folder android

Im doing a function where I record my voice then I save it into the data folder. Managed to work around saving into the data folder. But now my problem is that I need to check whether there is that specific data in the data folder so that I can do some stuff.. This is my code and not sure where went wrong:
individualFile = "y1";
OUTPUT_FILE= mydir + "/" + individualFile;
try{
File file = this.getFileStreamPath("y1");
if(file.exists()){
Log.v("Oh yes the thing is here", OUTPUT_FILE);
Share();
}else{
btn.setVisibility(View.VISIBLE);
}
}
catch(Exception e){
//Error message
}
}
What I need is that to check if the recorded file is already inside the data folder and if it is, then i need to show the Share() else I need to show the button for recording.

Categories

Resources