创建一个工具,在Android平板电脑上打开本地pdf文件

时间:2015-03-05 09:44:01

标签: javascript android html pdf

如果这是一个愚蠢的问题,我很抱歉,但我对这个问题的了解非常有限。我不是要求一个完整的解决方案,我只需要朝着正确的方向努力。

我在SD卡上有很多pdf文件的adroid平板电脑。现在我想制作一个工具来扫描带有文件名的条形码并打开这个pdf文件。条形码将类似于test,相应的文件将为file://sdcard/pdf/test.pdf。 条形码扫描器在扫描后给出一个输入,所以我只需要屏幕上的文本框和输入键来启动打开活动。我们宁愿不将此平板电脑连接到我们的网络,因此它没有互联网连接。

我已经尝试为此制作应用程序,但由于我的Android编程技能非常有限,这对我来说不起作用。 现在我正在尝试使用php或javascript创建一个Web应用程序。这对我来说并不合适,因为我对其中任何一个的经验也是有限的。最初我在考虑使用VBA中的MS Access工具,但由于Android没有运行时,这个想法无法解决。

我已经搜索了一个解决方案,但没找到任何可以使用的解决方案。

有没有人对我应该使用的工具有什么建议?也许让我走向正确的方向。有关详细信息,请询问。

更新1:

现在我已经使用HTML和JavaScript创建了一个网页。我的代码如下:

<!DOCTYPE html>

<html>
  <head>
    <meta content="text/html; charset=windows-1252" http-equiv="content-type">
    <script type="text/javascript">
        function clickyClick() {
          url = 'file:///mnt/sdcard/documents/instructies/' + document.getElementById("barcode").value + '.pdf'
            window.open(url, '_blank');
          document.getElementById("barcode").value = "";
        }
</script> <title>Open werkinstructie</title>
  </head>
  <body>
    <form><span style="font-weight: bold; font-family: Helvetica,Arial,sans-serif; font-size:50px;">Barcode:</span>
      <input autofocus="autofocus" style="font-size:50px" name="barcode" id="barcode"

        onkeydown="if (event.which == 13) clickyClick()" type="text"> </form>
    <br>
    <button style="height: 85px; width: 120px; font-size: 30px; font-weight:bold;"

      onclick="clickyClick()">Open</button>
  </body>
</html>

我在平板电脑上使用Firefox打开。目前我无法使用条形码扫描仪或输入键使其工作,但按钮确实有效。此代码在Adobe Reader中打开文件,这正是我想要做的。

更新2: MainActivity.java:

package com.example...............;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final String TAG = null;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText mEditText = (EditText) findViewById(R.id.editText1);
        mEditText.setImeActionLabel("Klaar", KeyEvent.KEYCODE_ENTER);
        EditText.OnEditorActionListener exampleListener = new EditText.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                Log.d(TAG, "onEditorAction, key=" + event.getKeyCode() + " action=" + event.getAction());
                if (event.getAction() == KeyEvent.KEYCODE_ENTER) {
                    openPDF(mEditText.toString());
                    mEditText.setText("");
                }
                return true;
            }
        };
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void openPDF(String fileName){
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(baseDir + File.separator + "documents/instructies/"+fileName+".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(this, "No reader",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

更新3: MainActivity.java

package com.example..............;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final String LOG_TAG = "MainActivity";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText mEditText = (EditText) findViewById(R.id.editText1);
        mEditText.setImeActionLabel("Klaar", KeyEvent.KEYCODE_ENTER);
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Log.d(LOG_TAG, "onEditorAction, key=" + event.getKeyCode() + "action =" + event.getAction());
                if (event.getAction() ==1 && event.getKeyCode() ==66) {
                    String Text = mEditText.getText().toString();
                    openPDF(Text);
                    mEditText.setText("");
                }
                return true;
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void openPDF(String fileName) {
        File file = new File("sdcard/documents/instructies/" + fileName + ".pdf");
        //File file = new File("sdcard/documents/instructies/0195476.pdf");
        //final String LOG_TAG_2 = "MainActivity";
        //Log.d(LOG_TAG_2, "exist = " + file.exists());
        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(this, "No reader",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

在你的布局中添加一个EditText:

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    >
    <requestFocus />
</EditText>

在您的活动中,在onCreate方法中添加:

final EditText mEditText = (EditText) findViewById(R.id.editText1);
mEditText.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER);
EditText.OnEditorActionListener exampleListener = new EditText.OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId,
            KeyEvent event) {
        Log.d(TAG, "onEditorAction, key="+event.getKeyCode() + " action="+event.getAction());
        if (event.getAction() == KeyEvent.KEYCODE_ENTER) {
            openPDF(mEditText.toString());
        }
        return true;
    }
};

要打开PDF,请在市场和活动中安装PDF应用程序,打开PDF格式:

private void openPDF(String fileName){

    File file = new File("/sdcard/pdf/"+fileName+".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(this, "No reader",
                    Toast.LENGTH_SHORT).show();
        }
    }
}