How to save map to sdcard - android

In my application I need to save map to sdcard. I am able to create directory and file, but in that file nothing is saved.How to save the map which is displayed? Please help me to solve my issue...
File myDir=new File("/sdcard/ma");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
I added permissions in android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related

java.io.FileNotFoundException: /storage/emulated/0/saved_images/grub.jpg: open failed: ENOENT (No such file or directory)

Am using the below code to save an image in sd card but I keep on getting this below exception
private void SaveImage(Bitmap finalBitmap,String filename) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = filename;
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Am I missing out anything here?
If you face this problem in Android version 10 then Open the manifest file and add this line to your application tag.
<application android:requestLegacyExternalStorage="true" .....>
This issue is because of the introduction of scoped storage introduced in Android 10. And make sure that you add permission requests in manifest and take runtime permission from the user. You need runtime permission from the user in latest android versions.
Modify your code, as you are not creating the directory:
private void SaveImage(Bitmap finalBitmap,String filename) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = filename;
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
file.createNewFile();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Starting with SDK version 30, you can NOT extend android:requestLegacyExternalStorage="true". Instead modify your ImageLoader library a little. I see you have to modify your file path: File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "saved_images"); into something like File dir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "saved_images");. This should do a trick.
This is how I read a file from storage when I was checking for a timestamp in a text file. Line 2 is probably the best bet for you.
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/path");
dir.mkdirs();
File file = new File(dir, ".storage.txt");
Reader pr;
String line = "";
try {
pr = new FileReader(file);
int data = pr.read();
while (data != -1) {
line += (char) data;
data = pr.read();
}
pr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//do stuff with line
From Android 6.0.0 you need to use this code :
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
make sure you placed the permissions on Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Writing File in External SD card in android

I have tried so many ways to write file in external as card but not working. Please suggest me what to do.
The code snippet that I wrote is as follows:
final String directoryFile = "file:///"+"mnt/extsd/Test";
String filename = "TextData.txt";
Context context;
//String file=Environment.getExternalStorageDirectory()+ "/TextData.txt";
//String file = "mnt/extsd/TextData.txt";
//String file=Environment.getExternalStorageDirectory()+ "/RudimentContent/test.txt";
//File root = android.os.Environment.getExternalStorageDirectory();
//File dir = new File (root.getAbsolutePath() + "/download");
//File path = Environment.getExternalStorageDirectory();
//String filename = "TextData.txt";
//String fileName = "TextData.txt";
//String path = "Environment.getExternalStorageDirectory()/TextData.txt";
//File path = Environment.getExternalStorageDirectory();
public void onClick(View v)
{
// write on SD card file data in the text box
// dir.mkdirs();
//File file = new File(dir, "myData.txt");
//String fileName = surveyName + ".csv";
//String headings = "Hello, world!";
//File file = new File(path, fileName);
//path.mkdirs();
//OutputStream os = new FileOutputStream(file);
//os.write(headings.getBytes());
//create path
//create file
//File outFile = new File(Environment.getExternalStorageDirectory(), filename);
//File directoryFile = new File("mnt/extsd", "Test");
//directoryFile.mkdirs();
//create file
//File file = new File(Environment.getExternalStorageDirectory(), filename);
try{
File myFile = new File(directoryFile, filename); //device.txt
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),"Done writing SD "+myFile.getPath(),Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
I have commented on so may tried codes also. When I write in internal sd card then its working but not with external. Please suggest.
I had this before.
The reason you're having this exception is due to some bizarre ways the framework handles files and folders.
on my case was that I was testing, and all was working, and I deleted the testing folder and since then the system keeps trying to write on the deleted folder. I removed the project from the phone and reboot it and started working again.
furthermore, I suggest you a quick reading on this answer What is the best way to create temporary files on Android? and the comments of this answer... as there is a lot of useful information if you want to create a good app.
just set permission like this
android.permission.WRITE_EXTERNAL_STORAGE
If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory).
This method will create the appropriate directory if necessary.
If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:
/Android/data//files/
You will have to set the permissions too:
android.permission.WRITE_EXTERNAL_STORAGE
try this
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
EDIT: By using this line you can able to see stores images in the gallery view.
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Need to copy image from res folder to gallery/folder

Ok, so i have Gallery Application, with lots of images in it(res/drawable).
On selection you can Set as Wallpaper button and you will have it.
Now i want to save with button SAVE TO PHONE or SD card this image selected. How can i manage that. Copying from res of application folder to Phone or SD card. Dont want to take it from ImageView but just copy the original from res to Phone.
try this code:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
and add in the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Follow the steps :-
Create Bitmap using BitmapFactory.decodeResource
Write the contents of Bitmap to an OutputStream using Bitmap.compress
Save the file to anywhere you want.
Code:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
file = new File(path, "image.jpg");
fOut = new FileOutputStream(file);
Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.xyz);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

Is it possible to create a new folder?

Is it possible to create a new folder for images (to be taken through my app) to be stored in, on the android phone? (Or SD Card as far as I'm concerned). I could name it what I like [Maybe the name of my app so the files are easily found] and then the images taken by launching the camera through my app will be stored there. I'm thinking it might have something to do with Uri's. (Just a guess.)
use this code
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
check this link Android saving file to external storage
Just use File operation for that..
File imageDirectory = new File("/sdcard/Images/"); // Path for location where you want to make directory, Internal or External storage
// have the object build the directory structure, if needed.
imageDirectory.mkdirs();
And in Application's manifest file put permission..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Why not?
String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/YourAppRootDir";
File dir = new File(path);
dir.mkdirs();

How to save paints aswell as that background into sdcars

My Android application is supposed to save paints and a background image on my device's SD Card. I am using the method below inside my view class, but for some reason the SD Card is only saving the paints. I can't figure out why the background image isn't saving.
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
This is where I'm attempting to save the background, but it isn't working. Any ideas would be greatly appreciated.

Categories

Resources