android : how to make my apps open specific PDF file on click - android

I am wondering if my codes here can view a specific pdf file using existing PDF viewer in Android Phone (HTC Desire).. If i would like to open pdf files from local folder.. What should i do?
public class ghcm_Submenu1 extends Activity {
private ListView lv1;
private String lv_arr[]= {"item1", "item2"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.submenu);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parentView, View childView, int position, long id) {
if ((position)== 0){
Intent intent = new Intent();
File file = new File("/sdcard/item1.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ghcm_Submenu1.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
else if ((position)== 1){
Intent intent = new Intent();
File file = new File("/sdcard/item2.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ghcm_Submenu1.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
}
public void onNothingSelected(AdapterView parentView) {
}
});
}
}

Related

How can I open the file in arrayadapter list after clicking?

I'm listing PDF files in folder. But I want them to be named as filename, instead of sdcard/mypath/files Also, I want to open them whenever I click them via PDF viewer. My code:
public class activity1 extends ListActivity {
private List<String> fileList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File("sdcard/mypath");
ListDir(root);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
fileList.add(file.getPath());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}}
public class activity1 extends ListActivity {
ListView lv;
private List<String> fileList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File("sdcard/mypath");
lv = getListView();
ListDir(root);
}
void ListDir(File f) {
File[] files = f.listFiles();
fileList.clear();
for (File file : files) {
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
viewPdf(fileList.get(i));
}
});
}
}}
and open the file in pdf default pdf viewer
private void viewPdf(String file) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + "mypath"+ "/" + file);
Uri path = Uri.fromFile(pdfFile);
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
}
Use the above code..
Hope it helps!
Below code will help to open the file with as these type. check this answer:
public static Intent getOpenFileIntent(Context ctx, String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
File file = new File(path);
Uri uri = Uri.fromFile(file);
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
boolean status = canDisplay(ctx, type);
if (type == null || !status) {
type = "*/*";
}
if (!status) {
Toast.makeText(ctx, "Cannot find appropriate Application", Toast.LENGTH_LONG).show();
}
intent.setDataAndType(uri, type);
}
} catch (Exception e) {
AppLog.exception(e);
}
return intent;
}
and below code will check that the particular actionview app is present or not
static boolean canDisplay(Context context, String mimeType) {
try {
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType(mimeType);
return packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
} catch (Exception e) {
AppLog.exception(e);
}
return false;
}
if the file type can openable, it will show the file with appropriate application or list the application can open the file.
UPDATE
Before send the file to this function make sure , that the particular file is present and readable.

onclick listview in oncreate()

I am getting the values and setting the adapter on onpostexecute() of my async task and when i click the items of listview in oncraete() Its showing nothing and I am trying to show a toast to test it.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list);
new Ftpclient().execute();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Toast.makeText(getApplicationContext(), "clicked on", 500).show;
String f = "FILE54.pdf";
File file = new File(Environment
.getExternalStorageDirectory()
+ "/FtpFiles"
+ f);
if (file.exists()) {
Uri filepath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(filepath, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"file not found", 500);
Log.e("error", "" + e);
}
}
}
});
class Ftpclient extends AsyncTask<String, Void, ArrayList<String>> {
ArrayList<String> temparrlist = new ArrayList<String>();
ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(MainActivity.this, "Connecting",
"please wait");
}
protected ArrayList<String> doInBackground(String... connection) {
temparrlist = listftpitems();
return temparrlist;
}
protected void onPostExecute(ArrayList<String> result) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
result);
lv.setAdapter(arrayAdapter);
dialog.dismiss();
}
}
Help is always appreciated,Thanks
EDIT
ArrayList<String> temparrlist = new ArrayList<String>();
temparrlist = listftpitems();
String uri = temparrlist.get(position).toString();
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/FtpFiles" + "/" + uri);
if (uri.endsWith(".pdf") || uri.endsWith(".txt")) {
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 (Exception e) {
Toast.makeText(getApplicationContext(),
"file not found", 500).show();
Log.e("error", "" + e);
}
This code...
Toast.makeText(getApplicationContext(), "clicked on", 500);
Should be like this...
Toast.makeText(getApplicationContext(), "clicked on", Toast.LENGTH_SHORT).show();
You have to use show() method to show toast notification.....
EDIT :
To use SPECIFIED TIME YOU CAN USE BELOW CODE..........
final Toast toast = Toast.makeText(getApplicationContext(), "clicked on", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 500);
EDIT :
To Open PDF USE FOLLOWING CODE...THAT WORKS FOR ME...........BUT MAKE SURE THAT YOU HAVE PDF READER IN YOUR MOBILE............
btn_open.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
name = edt_filename.getText().toString(); // name of selected file...
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/documents" +"/"+ name+".pdf"); // going to directory
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) {
// Instruct the user to install a PDF reader here, or something
}
}
});
change you toast:Toast.makeText(getApplicationContext(), "clicked on"+position, 500).show();

view pdf in android using program or javascript

I'm new to android development. I have a link in HTML page which is having a PDF document URL as h ref, while clicking the link i need to open the PDF in adobe reader. if any one knows how to do guide me.
Try this :
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();
}
}
}
});
}
}
Hope this helps.

How can I open a PDF file via my Android app?

I want to open a pdf file via an Android app. So far I have written the following code:
public class PdfopenActivity extends Activity {
String selectedFilePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Sample.pdf is my file name it is in /Root/Download/Sample.pdf
// path of the file
File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
}
The pdf is 175kb and I can open the file directly on my Galaxy Tab2 tablet, but when I run my my program to open it I get the error:
An Error Occurred while opening the document.
Can anyone tell me where I am going wrong?
.
Try this out:
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/sample.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) {
}
}
}
});
Hope this will help you.

Opening pdf in android/kindle app

I want to open a pdf from a website onto an app i'm making for the kindle and android.
I think my code is right but it can't find my file and I wasn't sure if anyone knew if I should be formatting the file different since it's technically a website....
Any input would be greatly appreciated. Here's what I have so far:
Button OpenPDF = (Button) findViewById(R.id.button1);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("http://thisisthewebsitewithpdf.pdf");
if(pdfFile.exists()) //EXCEPTION HERE. pdfFile doesn't exist!
{
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(PDFActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
you can download the pdf to your sdcard and then open it using this activitty:
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();
}
}
}
});
}
}

Categories

Resources