Unable to create a file in the Memory card - java

I'm trying to write data in the file and store it in the database. I tried out lots of codes online. But, I couldn't write the data. Can anyone please help me out. As of now I'm using the following code. I declared onClick="writemessage" and using the below code in the writemessage(View view) function. I'm running by connecting the mobile to the laptop. Can anyone please help me out.
FileOutputStream fos = null;
try {
String filename = "abc.txt";
String data = "Sensor data is found";
File myFile = new File(Environment
.getExternalStorageDirectory(), filename);
if (!myFile.exists())
myFile.createNewFile();
byte[] data = string.getBytes();
try {
fos = new FileOutputStream(myFile);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Check AndroidManifest.xml for this permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and in your activity handle run time permission
private int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
Check for Write External Storage Permission before writing file on SD card.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}else {
//Write Data to SD Card
}
and override onRequestPermission method of activity.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
this.finish();
Toast.makeText(this, "Permission is required.", Toast.LENGTH_LONG).show();
}
return;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}

Have you added this permission in manifest file and handle run time permission.
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related

Permission to save text in storage android not working

I have build sample app for user to write some text and save it on internal storage.
Format TXT or pdf. I have tried on android Kitkat sdk 19 and It's working fine but when I tried on the latest android 9 I got denied could not save the text in internal storage
Edit : I wonder If have to manually create My files folder
What I'm I missing ?
AndroidManifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSaveTextBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mText = mResultEt.getText().toString().trim();
if(mText.isEmpty()){
Toast.makeText(MainActivity.this, "write text First...", Toast.LENGTH_SHORT).show();
}
else{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions,WRITE_EXTERNAL_STORAGE_CODE);
}else {
saveToTxtFile(mText);
}
}else {
saveToTxtFile(mText);
}
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case WRITE_EXTERNAL_STORAGE_CODE: {
if (grantResults.length > 0 && grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
//permission granted save data
saveToTxtFile(mText);
} else {
//permission denied
Toast.makeText(this, "Storage Permission required to store", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private void saveToTxtFile(String mText){
//get current time for file name
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(System.currentTimeMillis());
try{
//path tp storage
File path = Environment.getExternalStorageDirectory();
//create folder named "My file"
File dir = new File( path + "/My Files/");
dir.mkdirs();
//file name
String fileName = "MyFile_" + timestamp + ".txt";
File file = new File (dir, fileName);
//used to store characater in file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(mText);
bw.close();
//show file name and path where file saved
Toast.makeText(this, fileName+"is saved to\n" +dir, Toast.LENGTH_SHORT).show();
}catch (Exception e){
//if anything goes wrong
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
As suggested both Shashanth and kashyap answers.
This is just a workaround but it works.
I have added
android:requestLegacyExternalStorage="true"
to my <application> element in the AndroidManifest.xml file.
And changed the line
File path = Environment.getExternalStorageDirectory();
to
File path = new File(getExternalFilesDir(null).getAbsolutePath());
In your MainActivity you should check if permission is granted or not, then use != operator.
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {}
this line should be-
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){}

How to read and write to SD card?

I have an app that can open and edit binary file from external SD card. I want to have possibility to save this file back from where it was opened.
I have added permissions in manifest file and I also ask user for permission.
Thanks to those permissions I can open file and get the data but when I want to save file to external SD card there is an error: java.io.FileNotFoundException: /storage/3834-3433/file.bin: open failed: EACCES (Permission denied).
Here is code for granting permission:
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
return true;
}
}
Here is the code for choosing and getting file path:
button.setOnClickListener(v -> {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 200);
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 200:
if (resultCode == RESULT_OK) {
String filePath = Objects.requireNonNull(data.getData()).getPath();
filePathMain = filePath;
}
break;
}
}
And this is the part of code to save file:
void byteArrayToFile() {
try (OutputStream out = new FileOutputStream(filePathMain)) {
out.write(outBytes);
} catch (IOException e) {
e.printStackTrace();
}
}
I have no idea why it allows me to open file but not to write when I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
I also had to run this app on the real device because when I run this on emulator there is an error that it can't find the file I choose.
I could really use some help on this. Thank You.
Based on #CommonsWare comment I changed from ACTION_GET_CONTENT to ACTION_OPEN_DOCUMENT to get a file Uri.
Also used ContentResolver to read:
InputStream inputStream = getContentResolver().openInputStream(uri);
and write to file:
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
And now it's working properly.

How do I Store a String to a file in android

I am trying to store a String to a file which should be stored on the device
I have the following code that stores the String into a variable called exportstring:
final JSONArray jsonArray1 = jsonArray;
JSONObject export = jsonArray1.getJSONObject(index);
String exportString = export.toString();
writeToFile(exportString);
I then have a function called writeToFile
private void writeToFile(String content) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.append(content);
writer.flush();
writer.close();
} catch (IOException e) {
}
}
when I run it, I get a message coming up saying "The file was saved"
but when I open a file manager, I do not see it anywhere.
where is the file saved to?
or have I done something wrong here?
To store the file in Downloads folder, change:
File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");
to:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test.txt");
UPDATE: Before trying to save the file you should make sure you have the required permissions, in this case the permission to write to storage. Below is a short example of how to do that:
protected boolean checkPermission() {
int result1 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result1 == PackageManager.PERMISSION_GRANTED ) {
return true;
} else {
return false;
}
}
protected void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "Write External Storage permission allows us to do store files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("permissionCheck", "Permission Granted, do what you want to do when user grants the permission here!!!");
} else {
Log.d("permissionCheck", "Permission Denied, do what you want to do when user denies the permission here...");
}
break;
}
}
Include the code above to your activity (or any activity before the one you currently are at) and before trying to store the string to the file do the following:
if(!checkPermission()){
Log.d("permissionCheck", "Need to get the permissions");
requestPermission();
}else{
Log.d("permissionCheck", "Already have the permissions");
}
To see the logs in logcat :

I canĀ“t copy my MP3 file from raw to external storage in android

I want copy a mp3 file to external storage, that i can share it over whatsapp or something like that.
I get some issues:
java.io.FileNotFoundException: /storage/emulated/0/Music/Arno lacht. (Is a directory)
How i can create a file? Im new in programming sry...
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNALSTORAGE:{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
// External Storage accessible?
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
try{
// Create path
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) ;
File root = new File(path, "Sounds");
if(!root.exists()) {
// Create new directory
root.mkdirs();
Toast.makeText(getApplicationContext(), "Ordner wurde erstellt", Toast.LENGTH_SHORT).show();
}
// Make sure the music directory exists.
File file = new File(root, soundNamenArray.get(itemPosition) );
// Copy data from internal memory to external memory
InputStream is = getResources().openRawResource(soundId[itemPosition]);
FileOutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
int length;
while (( length = is.read(data)) > 0){
os.write(data, 0, length);
}
is.close();
os.close();
Toast.makeText(getApplicationContext(), "Datei kopiert!", Toast.LENGTH_SHORT).show();
uri = Uri.fromFile(file);
}catch (IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception geworfen", Toast.LENGTH_SHORT).show();
}
// Intent zum verschicken Starten
teilenIntent = new Intent(Intent.ACTION_SEND);
teilenIntent.setType("audio/mp3");
teilenIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(teilenIntent, "Sound teilen"));
}else {
Toast.makeText(getApplicationContext(), "Fehler", Toast.LENGTH_SHORT).show();
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}

Android create directory in internal storage not works in Android 7.0?

I am using this code in async to create directory if not exists in android with permissions. My code sucessfully works in android 5.1 but when i deployed app in android 7.0 the directory not created automatically
File sdcard = Environment.getExternalStorageDirectory() ;
File folder = new File(sdcard.getAbsoluteFile(), "Quoteimages");
if(!folder.exists()){
folder.mkdir();
}
Manifest file is
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
Link to detail of application apk App apk detail link
Now what should i do i want to create folder write images from url to it and then read them. This work for me in android 5.1 but not in Android version 7 .
You need to implement runtime permissions for android versions above lollipop
Maybe this piece of code will help you:
private static final int PERMS_REQUEST_CODE = 123;
//...........................................................................................................
private boolean hasPermissions(){
int res = 0;
//string array of permissions,
String[] permissions = new String[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
return false;
}
}
return true;
}
private void requestPerms(){
String[] permissions = new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(permissions,PERMS_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
boolean allowed = true;
switch (requestCode){
case PERMS_REQUEST_CODE:
for (int res : grantResults){
// if user granted all permissions.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user not granted permissions.
allowed = false;
break;
}
if (allowed){
//user granted all permissions we can perform our task.
ListItem listItem = new ListItem();
Glide.with(getApplicationContext()).asBitmap().load(listItem.getImgurl()).into(new SimpleTarget<Bitmap>(){
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
FileOutputStream fileOutputStream = null;
File file = getDisc();
if(!file.exists()&& !file.mkdirs()){
Toast.makeText(getApplicationContext(),"Can't create directory to save image", Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyymmsshhmmss");
String date = simpleDataFormat.format(new Date());
String name = "img"+date+".jpg";
String file_name = file.getAbsolutePath()+"/"+name;
File new_file= new File(file_name);
try {
fileOutputStream=new FileOutputStream(new_file);
resource.getHeight();
resource.getWidth();
resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Toast.makeText(getApplicationContext(),"SAVED", Toast.LENGTH_LONG).show();
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
refreshGallery(new_file);
}
public void refreshGallery(File file){
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
private File getDisc(){
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(file, "FolderNamerYourChoice");
}
});
}
else {
// we will give warning to user that they haven't granted permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(this, "Storage Permissions denied.", Toast.LENGTH_SHORT).show();
}
}
}

Categories