I\'m creating an app for a private use only... it\'s not so important then, respect your time :P
What I want to make is an app that reads from a database and plays s
Here is the code @Nisarg, had to clean it a bit:
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity implements OnClickListener {
Button readExcelButton;
static String TAG = "ExelLog";
MediaPlayer mpintro;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Toast.makeText(this, "Permission checking", Toast.LENGTH_SHORT).show();
checkPermission();
}
readExcelButton = (Button) findViewById(R.id.readExcel);
readExcelButton.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.readExcel:
String filePath = getExternalFilesDir(null) + "/p0000.mp3";
File file = new File(filePath);
file.setReadable(true);
file.setWritable(true);
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Toast.makeText(this, "Ext storage not available or read only", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Ext storage available", Toast.LENGTH_SHORT).show();
}
if (file.exists()){
Toast.makeText(this, "File found", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, getExternalFilesDir(null) + "/p0000.mp3 - File not found", Toast.LENGTH_LONG).show();
}
break;
}
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
123);
} else {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 123: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Peform your task here if any
} else {
checkPermission();
}
return;
}
}
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}