【问题标题】:Parse the url to other Activity to load PDF from that url将 url 解析到其他 Activity 以从该 url 加载 PDF
【发布时间】:2018-01-01 02:16:29
【问题描述】:

我在应用中有两个活动。一个有 3 个按钮,另一个有来自 github“com.github.barteksc:android-pdf-viewer:2.6.1”的 PDFView 我正在尝试从此代码中的 url 加载 pdf。它对我有用

public class PDFActivity extends AppCompatActivity {

    PDFView pdfView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf);

        pdfView = (PDFView) findViewById(R.id.pdfview);

        new RetrievePDFStream().execute("my url to load pdf example http://sample.com/xyz.pdf");

    }


    class RetrievePDFStream extends AsyncTask<String, Void, InputStream>
    {

        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            try
            {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                if(urlConnection.getResponseCode() == 200)
                {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
            }
            catch (IOException e)
            {
                return null;
            }
            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream);
        }
    }
}

这工作正常,但我必须从其他 url 加载另一个 pdf 来自活动 1,当我单击按钮 1 以在此活动中加载另一个 pdf 和按钮 2 从中加载另一个 pdf 时更改该 url“新 RetrievePDFStream”活动。

非常感谢任何答案!

【问题讨论】:

    标签: java android-studio pdf url android-activity


    【解决方案1】:

    如果我理解正确,您有 3 个按钮和两个活动。
    在 ack1 中有 3 个按钮,在 ack2 中有 pdf 查看器。所以如果我点击 ack1 button1 你想从 ack2 加载 url1 如果我点击 ack1 button2 你想从 ack2 加载 url2 等等.... 如果这是要求,那么您可以使用意图附加功能。 表格 ack1 这样做:

    button1.onclick(new onclicklistener{
       publi void onClick(){
            Intent intent=new Intent(this,ack2.class);
            intent.putExtras("url","pdf url1");
        }}
    

    对于 button2 jst 将“pdf url1”更改为“pdf url2”
    对于button3 jst,将“pdf url1”更改为“pdf url3”

    在 ack2 中这样做: 在 onCreate() 方法中:

        Intent i=this.getIntent();
        String url=i.getExtras("url");
    

    从这里您将根据您单击的按钮获得您在第一个活动中传递的网址。如果用户单击按钮 1,您将获得“pdf url1”,如果他单击按钮 2,您将获得“pdf url2”.. 以此类推...

    编辑:代码仅供参考,请勿复制粘贴...

    【讨论】:

    • 不客气。如果有效,请投票给答案。请在发布问题之前彻底搜索。我想这已经被问过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2017-02-11
    • 2013-09-12
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多