Im open a epub file when I press a button, but when my android donĀ“t have any epub reader, show an toast saying please install epub reader, but my code not works, and stopp my app saying the app stop unexpectedly.
Button leer = (Button) findViewById(R.id.Btnleer);
leer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
File file = new File("/sdcard/Mi Biblioteca/"+nomb+".epub");
Intent intentreadf = new Intent(Intent.ACTION_VIEW);
intentreadf.setDataAndType(Uri.fromFile(file),"application/epub+zip");
intentreadf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentreadf);
try {
startActivity(intentreadf);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"favor descargar lector epub.",
Toast.LENGTH_SHORT).show();
}
}
});
Remove the first startActivity(intentreadf); command right after intentreadf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);. It gets called before your try-catch block, and hence throws an exception.
Related
I am working on Messages and issue which i am facing here is default messenger app is not opening in Lenovo K8 Note (7.1.1) on tapping the button and this issue occurs only in this device.I analysed forums but i didnt get a proper solution.Suggest me how to resolve this issue.
chat.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent sms = new Intent(Intent.ACTION_VIEW);
sms.setType("vnd.android-dir/mms-sms");
sms.putExtra("sms_body_popup",
getResources()
.getString(R.string.custom_refer_message_chat)
+ "\n" + str_url);
startActivity(sms);
} catch (Exception e) {
Toast.makeText(
getApplicationContext(),
getResources()
.getString(R.string.no_default_apps),
Toast.LENGTH_LONG).show();
}
}
});
To answer your question, Can i expect some code snippet for your query.
Hi I am fairly an amateur at android so I might not be realizing something obvious.
I have a method that populates a global File Array variable with a list of flies in a specific directory. Problem is everything works fine if the directory has been made before by using my app to save a file there however when the user hasn't done that an error message is suppose to pop up saying they haven't saved a file yet.
I do a check if the directory exist but the app crashes when the directory has not been created.
This is what my code looks like any assistance would be appreciated
private void getTemplates()
{
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.exists())
{
templatePaths = finalMarkTemplateDir.listFiles();
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
}
I am too an amateur, you have not created a file in your code, calling a new file() method does not create a file. Pls check that out
try {
finalMarkTemplateDir.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I managed to solve my problem when I call the setResult and finish methods I did not realize the flow of the program is returned to my onCreate method which meant the rest of my method calls in onCreate was still being called and they require the templatePaths array.
So basically I thought finish would stop the processing and move back to the calling class(using startActivityForResult). Instead I now call finish from my onCreate and use a boolean to determine if I could successfully access the directory.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_load_template);
boolean fileLoadStatus = getTemplates();
if(fileLoadStatus)
{
populateTemplateList(templatePaths);
}
else
{
setResult(RESULT_CANCELED);
finish();
}
}
private boolean getTemplates()
{
boolean fileLoadStatus = false;
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.isDirectory())
{
templatePaths = finalMarkTemplateDir.listFiles();
fileLoadStatus = true;
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
}
return fileLoadStatus;
}
I am new with android development.I am creating an app that will tell the meaning of name entered by user, for this I am using www.babynamesworld.parentsconnect.com to fetch the data and parse the meaning of name out of it.Here is the code but it doesn't seem to work.
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new Button.OnClickListener()
{
TextView tv=(TextView)findViewById(R.id.textView1);
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
EditText et=(EditText)findViewById(R.id.editText1);
Document document = Jsoup.connect("http://babynamesworld.parentsconnect.com/meaning_of_"+et.getText().toString()+".html").get();
Element firstMeta = document.select("meta").first();
String title = firstMeta.attr("DESCRIPTION");
tv.setText(title);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
As soon I click on the button, program crashes. I've added internet access permission too.
It looks like the Virtual Machine can't find the Jsoup class definitions. Have you included either the source files or a jar in your project?
If you've already added the jar try cleaning the project.
I am working on an application where need to open the pdf file in the device,
I have actually got the code on the web that is similar to most of the examples. But, the thing is that I am not able to open the file and the control goes to the "Exception" part directly.
Here is the code below:
public class MyPDFDemo extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
When I run this code : i used to see " No Application available to view pdf ". Can anyone please me to view the pdf file.
Since your catch block has ActivityNotFoundException, it means that you don't have any activity/application that can read a 'application/pdf' filetype format. Install any pdf viewer from the Android Market (Adobe recently release theirs), or else use the above mentioned open source pdf viewer and your problem will most probably will be solved.
http://code.google.com/p/apv/downloads/list
https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result
When you start activity with your given params, it searches for all applications/Activities/Intents that are registered to open pdf format. Since you have none in your device, you are getting the ActivityNotFoundException
First install the pdf reader on device.
than use this code for reading pdf file from internal memory.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView tv = (TextView)findViewById(R.id.tv);
Button bt=(Button)findViewById(R.id.openbtn);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
tv.setText("No Application available to view PDF");
}
}
else
{
tv.setText("File not found");
}
}
});
}
Your code is right, I have also used same code to open pdf file within a viewer.
As you don't have a viewer installed to your device so it can't open without any viewer.
You can install Adobe reader for Android.
I can't open pdf file within emulator, so I have to test using my device.
I have successfully setup the Facebook Plugin by Jos located (https://github.com/jos3000/phonegap-plugins/tree/master/Android/Facebook) - but I can't seem to figure out a way to log the user out. Sure I could tell them to delete the App access on the website then try to login again and click on "Not you?" but I would really rather have a JS Function that does it for me.
Can anyone help provide some guidance on how to do this? I've looked through the files and it looks like there is a way to do it in the facebook.java but I just need to hack something together to connect it to webview. I'm not capable of doing so :) can anyone please help?
This solution is to disable the single sign on feature in the Facebook plugin
in FaceBook.java file
replace DEFAULT_AUTH_ACTIVITY_CODE in the Authorize method [2 overloads] by FORCE_DIALOG_AUTH
in FacebookAuth.Java file
append this to execute method [in the switch case section]
else if (action.equals("performLogout")){
this.performLogout(first);}
//Add this method to FacebookAuth.java class
public void performLogout(final String appid) {
Log.d("PhoneGapLog", "LOGOUT");
final FacebookAuth fba = this;
Runnable runnable = new Runnable() {
public void run() {
fba.mFb = new Facebook(appid);
fba.mFb.setPlugin(fba);
try {
fba.mFb.logout((Activity) fba.ctx);
fba.success(new PluginResult(PluginResult.Status.OK, ""), fba.callback);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
};
this.ctx.runOnUiThread(runnable);
}
//in facebook.js file add the following section
Facebook.prototype.Logout = function(app_id,callback){
PhoneGap.exec(callback,null, "FacebookAuth", "performLogout", [app_id]); };
// in your page add the following code
function LogoutClick() //on logout click
{
appId = "123" ; //your app Id
window.plugins.facebook.Logout(appId,CompleteLogout);
}
function CompleteLogout() //call back function
{
//do some logic for callback
}
//Enjoy..!!