How to Show Directories/Folders in Gallery in android? - android

I am making an app where first i create directory and then storing the images in that directory. I am able to see these directories (folders and sub-folders) containing images. But in Gallery , they are not visible.
Here is how i am making a directory.
File filePath = Environment.getExternalStorageDirectory();
String phNumber[] = // userNumber
String dir = "/My Main Dir/"+phNumber[0];
File imageDirectory = new File(filePath, dir);
if(!(imageDirectory.exists())){
imageDirectory.mkdirs();
}
Whenever i saved an image in that directory , m calling this:
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
The directory shows after when i restart my phone and wait for a long time. Is there anyway where i can show an immediate effect or so in my gallery.

Related

how to check file exist on sd card or not

i have a activity where i have a image view..i capture image and image get stored in sd card folder. at the time of capterd image a folder will create in sd card and the respective clicked image stored in sd card card folder. And absolute path of that image inserted in sqlite database. i get the table index of image and split the path to get file/image name..,,i want to know how to check that image name exist in sd card folder or not at the time of post image to server..
String trp=AppDBManager.w1; ///image name get in Aapdbmanager class variable w1
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(Environment.getExternalStorageDirectory()+ "/Retailsolution/"+trp+"");
if(dir.exists()){
//code
}
File root = new File(Environment.getExternalStorageDirectory(), mFolder);
if (root.exists()) {
File mFile;
mFile = new File(root, mFileNameOut);
if (mFile.exists()) {
//YOUR CODE
}
}
File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
Try this:
File file = new File(path);
if(!file.exists()){
//your code if file not exists
}else {
}

Saving file in sd card without extension and access that with extension

I am downloading a pdf file from server and saving it on sd card without extension (for security purpose so that normal user can't open that file from file manager).For e.g.- I am downloading abc.pdf and saving it on sd card with abc on below path
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "files");
And later I want to view that file by adding extension.
So when I am accessing that file from code by using below code then its gives error file does't exist..
String s=Environment.getExternalStorageDirectory() + "/files/" + "abc";
File pdfFile = new File(s+".pdf");
So how can I save file in sd card without extension and later open that file with extension.
Workaround for your problem is to rename your saved pdf file without extension to filename with .pdf extension & after completing/accessing view or read operation on your new pdf extension file again you can rename it to file without extension
Example Usage:
String currentFileName = Environment.getExternalStorageDirectory() + File.separator
+ "files" + File.separator + "abc";
String renamedFilename = currentFileName + ".pdf";
boolean isRenamed = renameFile(currentFileName, renamedFilename);
Log.d("isRenamed: ", "" + isRenamed);
if(isRenamed {
//perform your operation
}
Again rename the file if not in use (here exchange renameFile() parameters):
boolean renameAfterOperation = renameFile(renamedFilename, currentFileName);
Log.d("renameAfterOperation : ", "" + renameAfterOperation );
And here is renameFile(currentFilePath, renamedFilePath):
public boolean renameFile(String currentFilePath, String renamedFilePath){
File currentFile = new File(currentFilePath);
File newFile = new File(renamedFilePath);
boolean isrenamed = currentFile.renameTo(newFile);
return isrenamed;
}
In this way whenever you want to perform operation on saved file first rename it to .pdf & whenever it's not in use, again rename it without extension. Let me know if this works for you..

How can i create Internal folder in my app?

How can I create internal folder in my application to put the image captured in it
after that get the image folder path ???
inside Android you can create folder either on SD card (External Storage) or on internal memory.
the following command create a folder
file.mkdir()
so when you provide a file path like bellow
File file=new File(PATH/FOLDER_NAME);
it will create on specific location but if you do not provide path it will simply create folder on internal memory. which is basically
data/data/YOUR_APPLICATION_PACKAGE/
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
File file = new File(path, "/" + fname);
You can add whatever folder name you want after "DIRECTORY_MOVIES" or "DIRECTORY_PICTURES" or whatever
Simply you can add this code on your camera capture button
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Your Folder Name");
imagesFolder.mkdirs();
Calendar cal = Calendar.getInstance();
File photo = new File(Environment.getExternalStorageDirectory()
+ "/Your Folder Name", (cal.getTimeInMillis() + ".jpg"));
mCapturedImagePath = photo.getAbsolutePath();
Uri uriSavedImage = Uri.fromFile(photo);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, PICK_FROM_CAMERA);
The above code create a folder and save an Image into your SD card
path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES);
File file = new File(path, "/" + fname);
You can add whatever folder name you want after "DIRECTORY_MOVIES" or "DIRECTORY_PICTURES" or whatever - and it's internal to the device, and not external like on an SD card. It simply means that other apps can see this data and that it's not internal to the app and invisible to other apps or users

Move Images to gallery programmatically in Android

This question may have been asked elsewhere but did not work for me.
I save images in the app-private folder and the user has the option to move it to Android gallery. So my question is how to do that?
This is my code
String outPath = "new path which is gallery";
File outFile = new File(outPath);
// File (or directory) to be String
sourcePath="image in the app-private folder";
File file = new File(sourcePath);
// Destination directory
boolean success = file.renameTo(new File(outFile, "name of image"));
System.out.println("success: "+success);
success here is always "false"

How to open a directory saved on SD Card Android

I am programming a notepad android app but I am having difficulties with my Open button. How do i implement it so that when I click on it, a dialog box comes up with the .txt files saved in the folder I created on the SD card? Also, how do i load the chosen file to my current activity?
Thank You
Try the Following,
File f = new File(Environment.getExternalStorageDirectory() +"/"+ dirname);
if(f.isDirectory())
{
ArrayList<String> files= new ArrayList<String>();
File file = new File(Environment.getExternalStorageDirectory() +"/"+ dirname+"/");
File fileList[] = file.listFiles();
for(int i=0;i<fileList.length;i++)
{
files.add(filelist[i].getAbsolutePath());
//here you can get all files.
}
}

Categories

Resources