简体   繁体   English

按钮onclick查看pdf文件Android

[英]Button onclick view pdf file Android

Hi I am trying to code a button in Android Studio that will launch a pdf file. 嗨,我正在尝试在Android Studio中编写一个将启动pdf文件的按钮的代码。 I have made a new afile under "res" folder and I am hoping that the button will call on that pdf file. 我在“ res”文件夹下创建了一个新的afile,我希望按钮会调用该pdf文件。 However it keeps coming up pdf not found? 但是它不断上升pdf未找到? Is there something wrong with my code? 我的代码有问题吗?

Button btn=(Button)findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener()
        {
          public void onClick(View v) {
           File file = new File("@Eng_Curr/Eng_Curr.pdf");
           Intent intent = new Intent(Intent.ACTION_VIEW);
           intent.setDataAndType(Uri.fromFile(file),"@Eng_Curr/Eng_Curr.pdf");
           intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
           startActivity(intent);
            }
        });

Is there something wrong with my code? 我的代码有问题吗?

Three things: 三件事:

  1. "@Eng_Curr/Eng_Curr.pdf" is not a path to a file on Android, yet you are trying to use it with the File constructor as if it is were a path. "@Eng_Curr/Eng_Curr.pdf"不是Android上文件的路径,但是您尝试将其与File构造函数一起使用,就好像它是路径一样。

  2. "@Eng_Curr/Eng_Curr.pdf" is not a MIME type, but you are passing it into setDataAndType() as if it were a MIME type. "@Eng_Curr/Eng_Curr.pdf"不是MIME类型,但是您将其传递到setDataAndType() ,就好像它是MIME类型一样。

  3. You do not have a PDF file, but rather some resource ( raw , I assume), and there is no easy way to get that to a PDF viewing app. 您没有PDF文件,但是有一些资源(我想是raw ),没有简单的方法可以将其获取到PDF查看应用程序。

If you are just trying to play around a bit on Android, I would recommend trying some other app. 如果您只是想在Android上玩一些游戏,建议您尝试其他一些应用。

If you really need to serve up a PDF file that you ship with your app as a raw resource, you can arrange to copy it to a file on internal storage (eg, getFilesDir() ) when your activity is first run, then use FileProvider to make that file available to third-party apps, and set up your Intent to use a Uri supplied by FileProvider . 如果您确实需要将应用程序随附的PDF文件作为原始资源提供,则可以安排在首次运行活动时将其复制到内部存储中的文件(例如getFilesDir() ),然后使用FileProvider使该文件可用于第三方应用程序,并设置您的Intent以使用FileProvider提供的Uri

Hi Folks so after taking all of your advice which is much appreciated I have found a work around for this. 嗨,大家好,在接受了您的所有建议后,我发现可以解决这个问题。 I used a swipe feature to swipe between png images and mViewFlipper along with SwipeGestureDetector. 我使用了滑动功能在png图像和mViewFlipper以及SwipeGestureDetector之间滑动。 However one of the problems with this is memory shortage. 但是,此问题之一是内存不足。 I'm pretty sure it is down to the size of the png files but I may also need to find a way to terminate saved instances after each swipe. 我很确定它可以缩小到png文件的大小,但每次滑动后我可能还需要找到一种方法来终止保存的实例。 Still working on it and any suggestions would be helpful. 仍在努力,任何建议都将有所帮助。 Anyway hope this code helps. 无论如何,希望这段代码对您有所帮助。

public class English extends Activity {

    public class English extends Activity { 
    private static final int SWIPE_MIN_DISTANCE = 520;
    private static final int SWIPE_THRESHOLD_VELOCITY = 500;
    private ViewFlipper mViewFlipper;
    private Context mContext;

 private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infant_eng);
        mContext = this;
        mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);




        mViewFlipper.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                return true;
            }
        });
    }

    class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                // right to left swipe
                if (e1.getX() - e2.getX()> SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
                    mViewFlipper.showNext();
                    return true;
                } else if (e2.getX() - e1.getX()> SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_out));
                    mViewFlipper.showPrevious();
                    return true;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM