I have searched everywhere for the answer to my problem and i did found a large amount of answer which should have solved my it but yet i can't solved it.
My Goal is to put a SD card with 128GB and to write files into it (csv or text). I'm trying to open a file or a folder in my microSD card but it do not create it. also the file is not created. I think the Path that i get from:
Environment.getExternalStorageDirectory()
is not The SD Card Path.
here is my java code :
package com.example.thermie.test2;
import android.Manifest;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.util.Date;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.os.Environment.DIRECTORY_DCIM;
import static android.os.Environment.getExternalStorageDirectory;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "";
private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Log used as debug
try {
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;
Toast.makeText(MainActivity.this,
"Read and Write", Toast.LENGTH_LONG).show();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(MainActivity.this,
"onlyread", Toast.LENGTH_LONG).show();
} 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;
Toast.makeText(MainActivity.this,
"cant writer", Toast.LENGTH_LONG).show();
}
String sdkS = String.valueOf(android.os.Build.VERSION.SDK_INT);
Toast.makeText(MainActivity.this,
sdkS, Toast.LENGTH_LONG).show();
String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
//create folder
File folder = new File(dir); //folder name
boolean result = folder.mkdirs();
//create file
File file = new File(dir, "filename.txt");
if (result){
Toast.makeText(MainActivity.this,
"True", Toast.LENGTH_LONG).show();
}
TextView textView = (TextView)findViewById(R.id.textView2);
textView.setText(dir);
Toast.makeText(MainActivity.this,
String.valueOf(state), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, "Error opening Log.", e);
Toast.makeText(MainActivity.this,
"not good", Toast.LENGTH_LONG).show();
}
}
}
My dir is : /storage/emulated/0/myDirectory Which is not in my SD card , it's the internal memory in an directory without any access.
my results of :
boolean result = folder.mkdirs();
is not true. so no folder opens , i also tried :
boolean result = folder.mkdir();
and my Toasts says:
- Read and Write
- 23 (API)
- mounted (sd mounted)
Phone galaxy s5. andorid version 6.0.1 Marshmallow.
Here is my manifeset :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thermie.test2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
Hope it is detail enough. Thanks you all.