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.
Related
I'm making an app where it uses intent to send data to another app. In case, the other app, which is supposed to receive data from my app, is not installed on users's device then it redirects user to play store with toast message asking user to install it. I used "if else" to achieve this. It worked all good until I found that if the other app is disabled by user (OEM installed app which can't be uninstalled), then my app crashes. In such a condition, I want to let user know that the app is disabled by them and ask them to enable it (through toast message). How can I achieve this?
Here is my complete code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
TextView textView = (TextView) findViewById(R.id.location);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.google.android.apps.maps");
if(isAppInstalled){
Uri gmmIntentUri = Uri.parse("geo:00,0000,00,0000");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else {
Uri uri2 = Uri.parse("market://details?id=com.google.android.apps.maps");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri2);
Toast.makeText(MainActivity.this, "Google Maps not Installed", Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
}
I'm not sure if this will work for you, but since you know the package name, you could try this to do a check beforehand.
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");
I have already created a QR scanner android application. What I need is that if I scan a QR code and if it generates a link, so the app should automatically open that link through the browser.
Any help will be appreciated. Thank You!!
When you get the callback of QR code generated
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
To open that link in browser
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
To validate the URL
URLUtil.isValidUrl(url)
If you have already implemented a "QR scanner" then check, there must be a callback method where you receive the returned text from the "Scanner".
Inside that callback method, fire an intent to open the browser with a specific url like shown below:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url)); // here ulr is the one that you get from scanner
startActivity(i);
I hope it helps.
Down below is my QR scanner code :
public class VehicleReaderActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("SCAN QR CODE");
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
// Do anything with result here
Log.w("handleResult", result.getText());
// Now direct the generated link to the browser //
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + result.getText()));
startActivity(browserIntent);
//Resume scanning
//mScannerView.resumeCameraPreview(this);
}
}
I am making an app called ROME, about the city of Rome. I have a activity called eten which means food and I want the activity to open a certain pdf-file called etenlijst.pdf when openened.
I got to the following code:
public class Eten extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eten);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
/**** This looks like a good place for it *****/
Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/ROME/Etenlijst.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(Eten.this, "Installeer een geschikte applicatie om PDF's mee te openen", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
But whenever I go to this activity in my app or click the button it won't open the file or give the toast?
Do you now why?
Thanks in advance,
Ide
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