Android : Reading a file from my app by just clicking on it - android

I have this issue:
I want to create an Android App that will read a certain file type (as is the case in PDF or MP3 players), the problem is that I dont know how to read a file with my application just by clicking on it
if anyone knows how to do this in android please help
i well be grateful for your help, thank you for your time and consideration and so sorry for my english

String outputFile = "my_patch.mp3";
button_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this content
MediaPlayer m = new MediaPlayer();
try {
m.setDataSource(outputFile);
}
catch (IOException e) {
e.printStackTrace();
}
try {
m.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
m.start();
}
});
and open file pdf, you make library pdf-view to android studio https://github.com/jblough/Android-Pdf-Viewer-Library
good luck..

Related

How to open word document file in android?

I have a word document(.doc or .docx) in the server. I need to open in my android application by clicking one button .doc file have to open. How to do this? anyone, please help me.
I tried this code :
File file = new File(Environment.getExternalStorageDirectory(),
"Document.docx");
try {
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
// replace "application/msword" with
// "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
// for docx files
// objIntent.setDataAndType(path,"application/msword");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(PMComplitionProjectActivity.this, "File NotFound",
Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(PMComplitionProjectActivity.this,
"No Viewer Application Found", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
}
I have this link that shows how to open any file in Android : http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html
Also, there is a similar case, which is done by PDF file : Android : how to open pdf file from server in android
I hope these links will work for you.
Good Luck,
G.

How to check a file before delete in android

I know how to delete a file, it's:
File file = new File(path);
file.delete();
Can I test that the file is currenlty in use without rooting my device?
For exmple I want to check if the file is open before i can delete it.
I want to be able to catch errors with a try catch sentence.
You can use a try/catch clause.
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can find more information here.

MediaPlayer - can't find path of sdcard on real device

On my code I use:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
And on the emulator the song.mp3 is played normally. But when I test it on my real device, it gives an error (-38, 0). That means it can not find the path of the song. I connect the usb cable, go to my Computer, GT-I8260 and paste the folder "mymusic" (that contains asong.mp3) under "Card" folder (where an empty folder named "LOST.DIR" is also placed). But why doesn't it work? Thanks a lot
it's card but at least
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
is enough.
Make sure it exists because you may not have the folder created before.
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
also make sure that it's not mounted while writing, since it may happen that it is not accessable at all.
Also revalidate that you have setupted the manifests permission to read/write the external storage

How to open any pdf file inside app?

I'm new in android development so I know how to open pdf from assest folder file by using some of the available API .
But how can I make for any pdf means like adobe reader.
If you would like to open Adobe Reader (with a version) specifically from your app, you need to query it using PackageManager.getPackageInfo(String, int)!
You can also try the following code,
private void loadDocInAdobeReader(String someDoc)
throws ActivityNotFoundException, Exception {
try {
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.parse(someDoc), "application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException ae) {
ae.printStackTrace();
throw ae;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

Saving a text from EditText into the Android internal Storage

So I want to save whatever users write in their EditText to be saved for the next time. This exact same code works for java project but with Android project, it doesnt work.
The code is below.
The PrintWriter out = new PrintWriter("hi"); always gives the FileNotFoundException e.
In java project, this code The PrintWriter out = new PrintWriter("hi"); makes a new file with the name "hi" but android project does not produce a new file instead returns the error. It does not save the String from the EditText to be opened when the app opens up again later.
Does anyone have a solution to this problem?
public void onCreate(blablabla)
{blablabla
try {
FileReader reader = new FileReader ("hi");
Scanner in = new Scanner(reader);
String line = in.nextLine();
mEditText.setText(line);
in.close();
} catch (FileNotFoundException e) {
mEditText.setText("");
Toast.makeText(mContext, "null!!", Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy()
{
super.onDestroy();
// The activity is about to be destroyed.
try {
PrintWriter out = new PrintWriter("hi");
out.write(mEditText.toString());
out.close();
} catch (FileNotFoundException e) {
Toast.makeText(mContext, "Can't save", Toast.LENGTH_SHORT).show();
}
}
May be Android permission problem. See http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms
Or better to use SharedPreferences?

Categories

Resources