App that plays audio file from internal Storage, Android Studio (JAVA) - java

I have attempted to create an App that plays wavfiles that have been created from another app I have made that records wavFiles.
The wav files I would like to play are stored in the following directory with my android device.
"/data/data/com.example.androidaudiorecorder/files/"
Here is the code I have attempted so far :
public class MainActivity extends AppCompatActivity {
Button playEvent ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playEvent= (Button) findViewById(R.id.btnPlayEvent);
playEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playEvent( );
}
});
}
public static void playEvent(){
try {
MediaPlayer player = new MediaPlayer();
player = new MediaPlayer();
player.setDataSource("/data/data/com.example.androidaudiorecorder/files/recording_DOG.wav");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}
}
}
}
Unfortunatley when I load the app and press play, nothing happens.
I am not sure if this is the correct way to use a file directory.
Any advice on how to fix this would be greatly appreciated.

1) You need to give permissions in the android manifest file to access storage before reading/writing any content int the storage .
2) To read the file,
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/filename.extension");

Related

Dynamically create play audio buttons

I have code that creates smaller wav file "events" from one larger wav file based on energy levels.
Depending on the structure of the orignial wav file the amount of wav file events created will vary.
I would like to create a dynamic amount of play buttons for each event that will play each event out loud.
The method to create an event has the following signature:
public void createEventWav(ArrayList<Double> event, String fileName, Context context) {
This will create a wav file with a unique filename.
This is the code I have currently that plays an event:
public Button playEvent;
playEvent= (Button) findViewById(R.id.btnPlayEvent);
playEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playEvent( );
}
});
public static void playEvent(){
try {
MediaPlayer player = new MediaPlayer();
player = new MediaPlayer();
player.setDataSource("/data/data/com.example.androidaudiorecorder/files/recording_DOG.wav");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}
I would like to have one for each event created.
Please see attched for layout of app
I have already tried many of the solutions found at : How to add a button dynamically in Android?
None have worked so far.

How to open a pdf in android by pressing a button?

I'm creating an app to open a PDF application from a button, but what I did is not going well.
In my layout there is only one button , which seeks want and open a PDF file.
Not giving error in Eclipse , just in tablet appears " readerPDF stopped ."
I do not know what to do. somebody help me.
Sorry for my english.
public class ReaderActivity extends Activity {
private Button btnOpen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
btnOpen.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
}
}
filename is null, so the path you are passing to the third-party app does not point to a PDF file.

Android: How to store input from edittext to a .csv

Fairly new to android, I come from a heavy fortran background. I've been trying to make apps, sucessful until now.
I'm having trouble finding a way for: saving an 'edittext' field by use of a button(save), then saving this user inputted data to a .csv file(preferably in internal storage).
I've found many articles but everyone glazes over the fundemental part I want(above).
the best idea I've got, is of generating the .csv in the class, then creating a method to save the 'edittext' as a new string, then to output that string to the .csv
Hopefully this can be simply explained, I just cant find this simple explanation anywhere, or at-least that I can understand...
Please try this.I hope this code helps you.
CSVFileWriter.java
public class CSVFileWriter {
private PrintWriter csvWriter;
private File file;
public CSVFileWriter(File file) {
this.file = file;
}
public void writeHeader(String data) {
try {
if (data != null) {
csvWriter = new PrintWriter(new FileWriter(file, true));
csvWriter.print(",");
csvWriter.print(data);
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
SampleActivity.java
public class SampleActivity extends Activity {
CSVFileWriter csv;
StringBuffer filePath;
File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
saveButton = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
filePath = new StringBuffer();
filePath.append("/sdcard/abc.csv");
file = new File(filePath.toString());
csv = new CSVFileWriter(file);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
csv.writeHeader(editText.getText().toString());
}
});
}
}
Add this in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Java- Error with Eclipse reading excel file

I'm new to java, but a quick learner.
I'm trying make my Android App to take user-input from an EditText, search an Excel column for a matching string, and return the value of (the same row of) another column.
I added a jxl.jar on the build path, and my code seems like it should work. Teaching myself, I am slowly figuring out the debugging. The problem seems to be source android.jar was not found (if i remember correctly). I located it, and now I get "the source attachment does not contain the source for the file instrumentation.class"
This is my first app using excel. Do I need to address the Android Manifest somehow?
Here's my code, just in case, and any help is very appreciated!:
public class Frame_Bore extends Activity {
private String inputFile;
public void setInputFile(String inputFile) {
this.inputFile = "c:/Desktop/AAS Work/PDA Programs/JBDB.xls";
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.framebore);
Button Btn1 = (Button) findViewById(R.id.button1);
final EditText T1 = (EditText) findViewById(R.id.et1);
final EditText T2 = (EditText) findViewById(R.id.et2);
Btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String t1Value = T1.getText().toString();
File inputWorkbook = new File(inputFile);
Workbook w;
String bore = null;
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet("Frame-Bore");
int y = 6;
while (y < 200) {
int x = 2;
Cell columnB = sheet.getCell(x, y);
String motorframe = columnB.getContents();
if (motorframe == t1Value) {
Cell columnC = sheet.getCell(x + 1, y);
bore = columnC.getContents();
} else {
y = y + 1;
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
T2.setText("" + bore);
}
}
});
}
You are trying to access c: drive ( "c:/Desktop/AAS Work/PDA Programs/JBDB.xls" ) from your emulator, which is not possible. You need to put your excel file either in the resources folder or in SD card, if you are running it in device.
An Emulator has a virtual SD Card, and its limitations is that it can not direct access any other drive from the computer. However you can do it by making a webservice which will perform operation on behalf of your android application. You need to call this web-service from your android application.
Put the file in assets folder and access it like this:
InputStream is = null;
try {
is = context.getAssets().open("JBDB.xls");
workbook = Workbook.getWorkbook(is);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
if(workbook!=null)
sheet = workbook.getSheet("Frame-bore");
context is your main activity context.

How to read PDF files on emulator of android

public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
I have tried this above code but no output can been seen on emulator.Please help me to solve my problem of reading a PDF on emulator
I think the issue is in the Activity where you're trying to show the .pdf.
I haven't tried opening any .pdf files in any of my apps, but a cursory search says there is no native, easy way to do it. In other words, there is no Android class that makes it a snap to show your .pdf files - so you'll have to use a third party library, or roll your own.
See Render a PDF file using Java on Android

Categories