Android File Transfer see my folder(created by app) as a file - android

I've installed "android file transfer" on mac. My app creates a folder.I can access it using standard file manager in my android device. It shows many folders, including my folder.
But the problem is that "android file transfer" shows me my folder as a file.
Unplugging the device or re-launch of "android file transfer" didn't help.
this is how i create my file:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
and I read that I needed this :
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{folder.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("tretretre",
"file " + path + " was scanned seccessfully: " + uri);
}
});
Can someone help me?

Late response, but I hope it can help someone.
I had same problem today. Seems that Android File Transfer app is handling last level folder's created by our apps as a file.
The only solution that has worked for me is to add a new last level folder inside the original one.
Instead of:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/");
Do:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/" + "myDummyFolder");
It could also be a hidden folder/file, like .myDummyFolder to avoid display it in file manager apps.

Related

Want to create Folder in File Manager of App but couldn't

Folder is creating Successfully but the location is something like storage/emualted/0
I'm using Enviroment.getExternalStorageDirectory methods
I want to create publicily available folder which i could save my app's data what should i do?
use Context#getFilesDir() orContext#getCacheDir()
it was restricted from google. below android 7 only u can create a folder inside internal folder. after that u only can create folder inside downloads,music and alarm
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(folder, "example.txt");
if (!file .exists()){
file .mkdirs();
}else{
}
Finally it worked in Android 10
//by Using This Method
private void createFolder(String name) {
File mydir = getApplicationContext().getExternalFilesDir(name); //Creating an internal directory;
if (!mydir.exists()) {
mydir.mkdirs();
Toast.makeText(this, "Created" + " -> " + mydir.getAbsolutePath(), Toast.LENGTH_SHORT).show();
} else {
}

Create a folder if it doesn't exist, then create a file and share it

I have an issue with my sharing option.
The option was perfectly well working (and I have saved it before trying some modifications) but I have tried to modify something and I cannot reach my goal).
The purpose is : click on the option in a menu, click on share, if the folder "test folder" doesn't exists in the "MUSIC" folder, create it, if it already exists, copie the sound, put it in the folder previously created and then use it as an extra in a SEND intent.
case R.id.share:
if (isStoragePermissionGranted()) {
File outputFile = new File("");
InputStream is = getResources().openRawResource(((Sound) adapter.getItem(index)).getMpsound());
try {
File outputDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Test folder");
outputFile = new File(outputDir, getResources().getResourceEntryName(((Sound) adapter.getItem(index)).getMpsound()) + ".mp3");
if (outputDir.exists()) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
OutputStream os = new FileOutputStream(outputFile);
os.write(buffer);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", outputFile));
share.putExtra(Intent.EXTRA_TEXT, "\"" + ((Sound) adapter.getItem(index)).getTitle_show() + "\" shared by my app");
startActivity(Intent.createChooser(share, "Share Sound File"));
} else {
outputDir.createNewFile(); //plus add code as below to share the sound after creating the folder
}
} catch (IOException e) {
e.printStackTrace();
}
}
I'm not able to create the folder and if I create it manually, the code is working well because the mp3 appears in the folder, and right after I have an error that says :
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Music/my app/sound_test.mp3
So... the app is able to access the folder to create the sound but for an unknown reason, I have this issue.
What did I do wrong ?
So, for the ones who are coming in few days, months, years with the same problem :
As CommonsWare said it, the issue in accessing the folder was from the FileProvider. After configure it properly with the good path, I had no more issue.
For the part concerning the creation of a new folder, .createNewFile() doesn't seems to be the right way. It's necessarry to use .mkdir()
And to end it, the part concerning the delete of the files in the folder, you will have your answer there :
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
Source : How to delete all files in a directory java android app? Current code is deleting the that folder

Rescan older files with mediascannerconnection in local storage. files get not updated on windows

My android-application writes a bunch of csv-files and jpg-files to the internal storage of the device. I am using MediaScannerConnection.scanFile() to make the files accessable from my windows-system without rebooting the android-device.
private void scanFiles() {
File targetDirectory = new File(Environment.getExternalStorageDirectory(), "DIR_OF_MY_APP");
if (targetDirectory.exists()) {
List<File> filesToScan = getFiles(targetDirectory);
List<String> filePathsToScan = new ArrayList<>();
for(File file : filesToScan) {
filePathsToScan.add(file.getPath());
}
MediaScannerConnection.scanFile(this, filePathsToScan.toArray(new String[0]), null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.d("OK", "Path: " + path);
Log.d("OK", "Uri : " + uri);
}
});
}
}
In my Logcat i can see every file is getting scanned. The new ones and the old ones.
My problem is when my app is adding new lines to an existing csv-file and the file is getting scanned, The new lines do not appear in the csv-file when its opend from my pc. How can i fix this problem?
I already tried to rename all the files from filename to tmp_filename, rescann all the files and rename them back from tmp_filename to filename and rescann them again. After this, i have can see the oldfilename-file and the tmp_oldfilename-file on my windows-computer. The tmp_oldfilename-file can not be opend (Unknown error on [memory-adress]). The oldfilename-file shows the not updated csv-file.
I also tried to use a intent to scan the files, since some questions on so say its going to update them:
for(File file : filesToScan) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Log.d("OK", "File: " + file.getName() + " scanned...");
}
here i can see the files getting scanned too, but they do not show up updated on my windows-computer.
Okay the only solution i came up with, is to set the usb-mode to load-only (this must be done by hand from the user) before performing the MediaScannerConnection.scanFile();. After this is done, the user can set the usb-mode back to mtp and than the csv-files will show up with the new added lines.
This is a really bad workarround, but still better than rebooting the device. If someone has an better solution, pls share.

Can't see my folder or files after creating in other applications

I have a small app that when I run it make a folder in my SD then save some Videos inside the folder
The thing is the app does the work and make the folder and save the files and I still can see it in SD using file manager or from my PC when I connect it via USB
But for e.g when I start Whatsup or my Gallery I can't see the folder or the videos !
I still can see the folder and videos that stored in the SD using File Manager
so what's wrong ? why I can't see the folder and videos in the Gallery or other programs when I want to attach the video ..
I used the same code link for my App to create the folder and check :
File folder = new File(Environment.getExternalStorageDirectory() + "/TollCulator");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
The Gallery works off of MediaStore, and WhatsApp may as well. Use MediaScannerConnection and its scanFile() static method to arrange to have your file(s) be indexed and available through the MediaStore, and see if that helps.
You need to Tell the media scanner about the new file so that it is immediately available to the user.
For example
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});

Android folder monitor

Am using sdcard for saving data ,my problem is that when app is running and some one delete this folder then the application fail. Is there anyway to notify the app , when this folder get deleted ?. Please help me
Try like this.
private static File logFile = null;
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
logFile = new File(SD_CARD_PATH + "/" + "folderName");
if (logFile.exists()){
//exist.so do your work
}
else{
//not exists
}

Categories

Resources