I am trying to save signature pad image to file but my app doesn't seem to be creating the directory in storage/emulated/0
this is what I am using to create the folder...
String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/Signature/";
String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
String StoredPath = DIRECTORY + pic_name + ".png";
and this is what I am using to create the directory
file = new File(DIRECTORY);
if (!file.exists()) {
file.mkdir();
}
and finally, this is where the file is saved...
public void save(View v, String StoredPath) {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(bitmap);
try {
// Output the file
FileOutputStream mFileOutStream = new FileOutputStream(StoredPath);
v.draw(canvas);
// Convert the output file to Image such as .png
bitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
}
Please note that this is not my own code...
(Basically a copy and paste from)
http://demonuts.com/android-capture-signature-using-canvas/
The pad works fine, I can write on it, but it won't save :(
When I send it to a Toast I can return the full directory and file name of Storage/emulated/0/Signature/20191101_a time stamp.png but it wont save on device.
Any ideas??
Declare the permissions in AndroidManifest.xml file, To grant external storage read write permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
For Android OS version > 6.0, you require run time permissions as well.
// Check whether this app has write external storage permission or not.
int permission = ContextCompat.checkSelfPermission(ExternalStorageActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
// If do not grant write external storage permission.
if(permission!= PackageManager.PERMISSION_GRANTED)
{
// Request user to grant write external storage permission.
ActivityCompat.requestPermissions(ExternalStorageActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION);
}
Thank you all for the help... my problem was creating a drive to save to. I found my answer at https://www.quora.com/How-do-I-create-a-folder-in-internal-and-external-storage-programmatically-in-an-Android-app
I should be able to create and save the file now thanks
Related
Problem
I am creating an Android app within Android Studio using Java. It needs a function that takes a bitmap image and saves it to a folder structure in internal memory. The images I'm working with are PNGs. My current implementation is based on the answer found here (Saving and Reading Bitmaps/Images from Internal memory in Android). I am very lost as to why not all of the log statements are being executed and no files are being created:
private String saveToInternalStorage( Bitmap bitmapImage, String dataType, int zoom, int x, int y )
{
Log.w(TAG, "Function Started.");
String imageDir = String.format( "%s/%d/%d", dataType, zoom, x );
String imageName = String.format( "%d.png", y );
ContextWrapper contextWrapper = new ContextWrapper( getContext() );
Log.w(TAG, "Passed Context Wrapper.");
// path to /data/data/app_name/app_data/<dataType>/<zoom>/<x>
// - - - NO LOGS SHOWN PAST THIS POINT - - -
File directory = contextWrapper.getDir( imageDir, Context.MODE_PRIVATE );
Log.w(TAG, "Passed Directory Creation.");
// Create image at imageDir (<y>.png)
File imageFile = new File( directory, imageName );
Log.w(TAG, "Passed File Creation.");
FileOutputStream outputStream = null;
Log.e(TAG, "Begining to Output to File.");
try
{
outputStream = new FileOutputStream( imageFile );
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress( Bitmap.CompressFormat.PNG, 100, outputStream );
}
catch( Exception e )
{
Log.e(TAG, "Error Adding Image to Internal Storage.");
e.printStackTrace();
}
finally
{
try
{
outputStream.flush();
outputStream.close();
}
catch( Exception e )
{
Log.e(TAG, "Error Closing Output Stream.");
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
The following two lines are the only relevant output shown in Logcat. I am not shown any errors and no other logs from the above function are shown.
2022-02-08 02:52:56.433 18914-18990/com.example.biomapper W/ContentValues: Function Started.
2022-02-08 02:52:56.434 18914-18990/com.example.biomapper W/ContentValues: Passed Context Wrapper.
What I've Tried
This is my third attempt at creating this function, and the closest I've gotten to making it work. I have looked at many other questions on Stack Overflow regarding this topic. I added many log statements to see where/what might be going wrong. I've tried using the file.exists() command to test if the file(s) were ever created. I am sure that the parameters given to this function when testing it are correct.
Any advice on saving images to internal storage, checking if the files exist, or improving my code in any way are greatly appreciated.
In This Way I Saved the image in android folder
Create a directory "xml" in res folder and paste
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.project/files/Pictures" />
</paths>
In Manifest file add this line under application
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_path" />
</provider>
initialize this in your respective activity
File photoFile = null;
Edit this function according to your requirements !!
try {
photoFile = createImageFile();
displayMessage(getBaseContext(), photoFile.getAbsolutePath());
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,"com.xyz.fileprovider",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST);
}
} catch (Exception ex) {
// Error occurred while creating the File
displayMessage(getBaseContext(), ex.getMessage().toString());
}
I am trying to have a local backup of a database of my app in the device storage. I created a backup file/directory, but I want user to restrict from being able to copy/delete the file/directory from the device.
Is it possible to achieve this through code, using a service which I am running?
Method to check if user has permissions to write on external storage or not.
public static boolean canWriteOnExternalStorage() {
// get the state of your external storage
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// if storage is mounted return true
Log.v(“sTag”, “Yes, can write to external storage.”);
return true;
}
return false;
}
and then let’s use this code to actually write to the external storage:
// get the path to sdcard
File sdcard = Environment.getExternalStorageDirectory();
// to this path add a new directory path
File dir = new File(sdcard.getAbsolutePath() + “/your-dir-name/”);
// create this directory if not already created
dir.mkdir();
// create the file in which we will write the contents
File file = new File(dir, “My-File-Name.txt”);
FileOutputStream os = outStream = new FileOutputStream(file);
String data = “This is the content of my file”;
os.write(data.getBytes());
os.close();
You need the following permission
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
Happy coding!!
I am developing a simple android application and I need to write a text file in internal storage device. I know there are a lot of questions (and answers) about this matter but I really cannot understand what I am doing in the wrong way.
This is the piece of code I use in my activity in order to write the file:
public void writeAFile(){
String fileName = "myFile.txt";
String textToWrite = "This is some text!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(fileName , Context.MODE_PRIVATE);
outputStream.write(textToWrite.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I really cannot understand which mistake I am doing. In addition, I have tried this project on my emulator in Android Studio and my phone in order to understand where I am doing something wrong but even with that project no file is written neither on the phone or on the emulator.
EDIT:
I know that no file is written to my internal storage because I try to read the content of the file, after I have written to it, with this code:
public void ReadBtn(View v) {
//reading text from file
try {
FileInputStream fileIn=openFileInput("myFile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
textmsg.setText(s);
} catch (Exception e) {
e.printStackTrace();
}
}
Nothing is shown at all.
Use the below code to write a file to internal storage:
public void writeFileOnInternalStorage(Context mcoContext, String sFileName, String sBody){
File dir = new File(mcoContext.getFilesDir(), "mydir");
if(!dir.exists()){
dir.mkdir();
}
try {
File gpxfile = new File(dir, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
} catch (Exception e){
e.printStackTrace();
}
}
Starting in API 19, you must ask for permission to write to storage.
You can add read and write permissions by adding the following code to AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can prompt the user for read/write permissions using:
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);
and then you can handle the result of the permission request in onRequestPermissionsResult() inside activity called from it.
no file is written neither on the phone or on the emulator.
Yes, there is. It is written to what the Android SDK refers to as internal storage. This is not what you as a user consider to be "internal storage", and you as a user cannot see what is in internal storage on a device (unless it is rooted).
If you want to write a file to where users can see it, use external storage.
This sort of basic Android development topic is covered in any decent book on Android app development.
Save to Internal storage
data="my Info to save";
try {
FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(), "file saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read from Internal storage
try {
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
tv.setText(temp);
Toast.makeText(getBaseContext(), "file read", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
Android 11 Update
Through Android 11 new policies on storage, You cannot create anything in the root folder of primary external storage using Environment.getExternalStorageDirectory() Which is storage/emulated/0 or internal storage in file manager. The reason is that this possibility led to external storage being just a big basket of random content. You can create your files in your own applications reserved folder storage/emulated/0/Android/data/[PACKAGE_NAME]/files folder using getFilesDir() but is not accessible by other applications such as file manager for the user! Note that this is accessible for your application!
Final solution (Not recommended)
By the way, there is a solution, Which is to turn your application to a file manager (Not recommended). To do this you should add this permission to your application and the request that permission to be permitted by the user:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Thoriya Prahalad has described how to this job in this stackoverflow post.
To view files on a device, you can log the file location >provided by methods such as File.getAbsolutePath(), and >then browse the device files with Android Studio's Device >File Explorer.
I had a similar problem, the file was written but I never saw it. I used the Device file explorer and it was there waiting for me.
String filename = "filename.jpg";
File dir = context.getFilesDir();
File file = new File(dir, filename);
try {
Log.d(TAG, "The file path = "+file.getAbsolutePath());
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
return true;
Have you requested permission to write to external storage?
https://developer.android.com/training/basics/data-storage/files.html#GetWritePermission
Perhaps your try\catch block is swallowing an exception that could be telling you what the problem is.
Also, you do not appear to be setting the path to save to.
e.g: Android how to use Environment.getExternalStorageDirectory()
To write a file to the internal storage you can use this code :
val filename = "myfile"
val fileContents = "Hello world!"
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
it.write(fileContents.toByteArray())
}
This Answer worked for me for android 11+ !! check it out, hopefully it'll work for you too
https://stackoverflow.com/a/66366102/16657358[1]
[(btw, int SDK_INT = 30; it confused me lol so thought i should mention)]
I have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my manifest file, however I fail when trying to create a directory
Log.d(LOG_STRING, android.os.Environment.getExternalStorageState() );
java.io.File folder = new java.io.File(Environment.getExternalStorageDirectory() + java.io.File.separator + "test");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
Log.d(LOG_STRING, "Created directory");
} else {
Log.d(LOG_STRING, "FAILED WHILE CREATING DIRECTORY");
}
The status of external storage is "mounted", but the test directory cannot be created and the output is "FAILED WHILE CREATING DIRECTORY".
Browsing in the phone to the "App Info", the permission "modify or delete the contents of your USB storage" is marked to be activated for my application.
What could be the cause of this? Some special setting of the phone? It's a Samsung GT-I9506 with Android 4.3 (API18). To be noted is that the getExternalStorageDirectory is not on the SD card, but on the internal storage (/storage/emulated/0/).
Update:
Speaking with colleagues, it seems that this device has undergone several tweaking after having been rooted (to allow a specific application to directly write on the SD card). It's probably not worth to investigate further, I will simply switch to another device. I'll keep the device for a while and if anybody will show up with an answer I will quickly test if it solves the problem.
Update 2: (Bounty end)
The problem remains unsolved, but as stated before, it is most likely something very specific to this one device. It's not possible to write on any path, being it external or internal storage, not even in the path returned by getExternalCacheDir().
In my case I had to go under Settings -> Apps -> NameOfApp -> Permissions and activate the Storage permissions. I don't know why this comes deactivated by default if I have all the permissions on AndroidManifest.xml. I'm on a Moto G (3rd Gen) with Android Marshmallow 6.0.
You first need to use file.mkdirs() instead of folder.mkdir().
And if it also not worked then you need to check if you can access the sdcard or not.
For checking you can use below method.
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
Read at official document .
Hope it will give some clue to you.
You can try this code. I have also emulated sd card on Lg g2 mini and I can only access external card in my package folder
for(File f : getExternalFilesDirs(null)){
try {
File f2 = new File(f.getAbsolutePath(), "testFile");
Log.d("test log", "file path " + f2.getAbsolutePath());
f2.createNewFile();
Log.d("test log", "file exist " + f2.exists());
} catch (IOException e) {
Log.d("test log", "error");
}
}
and output is:
D/test log? file path /storage/emulated/0/Android/data/my.awesome.package/files/testFile
D/test log? file exist true
D/test log? file path /storage/external_SD/Android/data/my.awesome.package/files/testFile
D/test log? file exist true
Try this:
final File externalStoragePublicDirectory = Environment.getExternalStorageDirectory();
File file = new File(externalStoragePublicDirectory, "test");
file.mkdirs();
I'm new to developing android apps. And already overchallenged with my first project. My app should be able to save a list of EditText fields to a text file by clicking a "save"-Button.
But I got no success to write a file to my SD-card.
My code:
(function in MainActivity.java called by the button)
public void saveData(View view){
try{
File sdcard = Environment.getExternalStorageDirectory();
// to this path add a new directory path
File dir = new File(sdcard.getAbsolutePath() + "/myapp/");
// create this directory if not already created
dir.mkdir();
// create the file in which we will write the contents
File file = new File(dir, "datei.txt");
FileOutputStream os = new FileOutputStream(file);
String data = "some string";
os.write(data.getBytes());
os.flush();
os.close();
}
catch (IOException e){
Log.e("com.sarbot.FitLogAlpha", "Cant find Data.");
}
}
With Google I found another way:
public void saveData3(View view){
FileWriter fWriter;
File sdCardFile = new File(Environment.getExternalStorageDirectory() + "/datafile.txt");
Log.d("TAG", sdCardFile.getPath()); //<-- check the log to make sure the path is correct.
try{
fWriter = new FileWriter(sdCardFile, true);
fWriter.write("CONTENT CONTENT UND SO");
fWriter.flush();
fWriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
In my manifest.xml I set the permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And the function from developer guide returns me True -> SD-card is writable.
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
In the res/layout/activity_main.xml are some TextViews and EditText and a save button with android:onClick="saveData" argument. The function is called. The SD-card is writable. And no IO errors. But after pressing the button (without error) there is still no new file on my SD-card. I already tried to create the file manually and just append but nothing changed. I tried some other function with BufferedWriter too .. but no success.
I'm running my Sony Xperia E with USB-Debug mode. Unmount and mounted the SD-card on my PC but cant find the file. Maybe it is only visible for the phone? It doesn't exist? I don't know what to do because I get no errors. I need the content of this file on my computer for calculations.
:EDIT:
The problem was not in the code.. just in the place I looked up. The externalStorage -> sdCard seems to be the internal and the removable sdcard is the -> ext_card.
After this line,
File file = new File(dir, "datei.txt");
Add this code
if ( !file.exists() )
{
file.createNewFile(); // This line will create new blank line.
}
os.flush() is missing in your code. Add this snippet before os.close()