Move Images to gallery programmatically in Android - 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"

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 {
}

Photo not saved in location given

I am trying to take a picture in my app, and then redisplay it. When I start the camera intent, I pass a URI to save the photo location in. Here is how I create the file:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "Waypoint_photo_" + timeStamp + ".jpg";
File storageDir = new File(PICTURE_FOLDER);
storageDir.mkdirs();
Log.v("FILE", String.format("New file: %s%s%s", PICTURE_FOLDER, File.separator, imageFileName));
return new File(storageDir, imageFileName);
Where picture folder is:
public static final String PICTURE_FOLDER = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "UtopiaWaypoints";
This creates a file similar to:
/storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_102116.jpg
In my onActivityResult, I get the file from the same path, and create a thumbnail version of it. At this point if I look on the device I see the files, but they are in /mnt/shell/emulated/0/Pictures/UtopiaWaypoints:
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ ls
Waypoint_photo_20150718_102825.jpg
Waypoint_photo_20150718_102825.png
shell#shamu:/mnt/shell/emulated/0/Pictures/UtopiaWaypoints $ pwd
/mnt/shell/emulated/0/Pictures/UtopiaWaypoints
On my device (nexus 6), /storage/emulated exists (neither is a symbolic link), but it only contains a "legacy" folder.
So when I try to load the file later from the /storage/emulated directory (which is what the Environment.PICTURE_DIRECTORY returns, the app can't find the file.
// This line outputs something similar to: /storage/emulated/0/Pictures/UtopiaWaypoints/Waypoint_photo_20150718_1042581116411973.png
Log.v("WPA", String.format("Loading thumbnail from: %s", Utils.getFullThumbnailPath(wayPoint.getPicture())));
Bitmap imageBitmap = BitmapFactory.decodeFile(Utils.getFullThumbnailPath(wayPoint.getPicture()));
wayPointImageView.setImageBitmap(imageBitmap);
The second to the last line (decodeFile) shows this in the log:
D/skia﹕ --- SkImageDecoder::Factory returned null
imageBitmap is null and the ImageView is blank.
What am I doing wrong?

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 to Show Directories/Folders in Gallery in 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.

where to write a text file and how to set it up for an sd card

I know this is a popular question but I have looked at all of the other responses and none of them seem to work. What I want to do is write some code to a text file. My first question: is there a way to view that text file without writing code to the console? Second, I dont know where in my phone it goes and I want to see it to help trouble shoot, so if you know how to do that too, it would be great. So now I will give you an overview of what is happening. When I start my program it checks to see if the file exists, if it doesn't it reads a file out of my assets folder and copies that info and sends it to a file into the sd card. If it exits it reads the info from the sd card. Next if I press a button and change numbers and print it to my sd card again then I close it using task managers then when I come back the original information is here. I don't feel like it is being able to find my sd card location. So far I have used.
File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath.getAbsolutePath()+"/TimeLeft/"+FileName);
File outfilepath = Environment.getExternalStorageDirectory();
String FileName = "ExSettings.txt" ;
File outfile = new File(outfilepath, FileName);
Any Ideas?
This is a function that I wrote which will take in a list array and write to a file line by line.
It will use the path in fileName to make a new folder called myfolder in the root of the SDCard, an inside will be your file of newtextfile.txt.
List<String> File_Contents = new ArrayList<String>();
File_Contents.add("This is line one");
File_Contents.add("This is line two");
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder/newtextfile.txt");
f.mkDirs();
try {
BufferedWriter out = new BufferedWriter(new FileWriter(f));
for (int x = 0; x < File_Contents.size(); x++) {
out.write(File_Contents.get(x));
out.write(System.getProperty("line.separator"));
}
out.close();
return true;
} catch (Exception e) {
return false;
}

Categories

Resources