【问题标题】:Can not open URL Android无法打开 URL Android
【发布时间】:2013-06-13 22:52:54
【问题描述】:

Android 初学者,我正在尝试创建一个 Android 应用程序以打开和阅读 PDF 文件。 然后,目标是能够向他们发送电子邮件并以数字方式对其进行签名。目前,我只是想通过按一个按钮来打开文件。 现在我的应用程序已启动!我看到我漂亮的“打开”按钮,我点击它并得到:

无法打开网址

嗯... 我在uri 周围尝试了一些东西,我试着理解:http://developer.android.com/reference/android/net/Uri.html 但没有成功...

您有什么想法可以帮助我吗?(我的应用程序使用有一些东西!)

这是我的代码:

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.pdfsigner"
      android:versionCode="1"
      android:versionName="1.0">

    <!--Limites de la version SDK -->
    <uses-sdk android:minSdkVersion="7"
        android:targetSdkVersion="7" />

    <!-- autorisation Internet -->
    <uses-permission android:name="android.permission.INTERNET" />

    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name">        
        <activity android:name=".PdfActivity"
                  android:label="@string/app_name">                  
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>
    </application>
</manifest>

PdfActivity.java

package com.example.pdfsigner;

import com.example.pdfsigner.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class PdfActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);

        //applicat° du Listener click/button1
        Button b1 = (Button)findViewById( R.id.button1 );
        b1.setOnClickListener( new OnClickListener() {

  //au click...         
  @Override
  public void onClick(View arg0) {
  String surl = "/ENGLISH.pdf";
  String apptype = "PdfSigner/pdf";
  if( openURLWithType( surl, apptype ) == false)
          Toast.makeText( PdfActivity.this, 
       "Echec de Open URL", Toast.LENGTH_LONG ).show();
                }
            }
        );
    }

    public boolean openURLWithType( String url, String type ) { 

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/R.raw.ENGLISH");

        Intent intent = new Intent( Intent.ACTION_VIEW, uri );
        intent.setDataAndType( uri, type );
        intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );

        try {
                PdfActivity.this.startActivity(intent);
                return true;
        }
        catch (ActivityNotFoundException e) {
                Log.e( "LTM" ,"Activity not found: " + url, e);
        }
        return false;
    }
}

activitymain.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".PdfActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="168dp"
        android:text="@string/Ouvrir" />
</RelativeLayout>

LOGCAT

06-13 17:33:46.402: E/LTM(17317): Activity not found: /ENGLISH.pdf 06-13 17:33:46.402:                    E/LTM(17317):
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=android.intent.action.VIEW
dat=android.resource://com.example.pdfsigner/R.raw.ENGLISH
typ=PdfSigner/pdf flg=0x4000000 } 06-13 17:33:46.402: E/LTM(17317):
    at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1638)
06-13 17:33:46.402: E/LTM(17317):   at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1510)
06-13 17:33:46.402: E/LTM(17317):   at
android.app.Activity.startActivityForResult(Activity.java:3258) 06-13
17:33:46.402: E/LTM(17317):     at
android.app.Activity.startActivity(Activity.java:3365) 06-13
17:33:46.402: E/LTM(17317):     at
com.example.pdfsigner.PdfActivity.openURLWithType(PdfActivity.java:48)
06-13 17:33:46.402: E/LTM(17317):   at
com.example.pdfsigner.PdfActivity$1.onClick(PdfActivity.java:31) 06-13
17:33:46.402: E/LTM(17317):     at
android.view.View.performClick(View.java:3538) 06-13 17:33:46.402:
E/LTM(17317):   at android.view.View$PerformClick.run(View.java:14330)
06-13 17:33:46.402: E/LTM(17317):   at
android.os.Handler.handleCallback(Handler.java:608) 06-13
17:33:46.402: E/LTM(17317):     at
android.os.Handler.dispatchMessage(Handler.java:92) 06-13
17:33:46.402: E/LTM(17317):     at
android.os.Looper.loop(Looper.java:156) 06-13 17:33:46.402:
E/LTM(17317):   at
android.app.ActivityThread.main(ActivityThread.java:4987) 06-13
17:33:46.402: E/LTM(17317):     at
java.lang.reflect.Method.invokeNative(Native Method) 06-13
17:33:46.402: E/LTM(17317):     at
java.lang.reflect.Method.invoke(Method.java:511) 06-13 17:33:46.402:
E/LTM(17317):   at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 17:33:46.402: E/LTM(17317):   at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-13
17:33:46.402: E/LTM(17317):     at dalvik.system.NativeStart.main(Native
Method) 06-13 17:33:49.915: D/memalloc(17317): /dev/pmem: Unmapping
buffer base:0x4ca72000 size:3694592 offset:3645440

谢谢!

【问题讨论】:

  • 你能在你的设备上打开你的应用之外的 pdf 文件吗?
  • @Krylez:是的,通过 adobe 阅读器

标签: android pdf pdf-reader


【解决方案1】:

要从电子邮件发送 PDF 文件,请查看此处Android Intent: Send an email with attachment

这个 Q 帮助你理解 uri What is the difference between a URI, a URL and a URN?

here you find how to create a BROWSE button, which when you will click, it will open up the SDCARD, you will select a File and in result you will get the File Name and File path of the selected one. to find the uri address you can use Log.d methood (go to the last answar)

一般结构如下:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

【讨论】:

  • 我不明白我的代码和你的(尤其是PICK_REQUEST_CODE)之间的关系。我认为,实际上,我没有成功地包括它应该做什么
  • PDF文件的地址是什么??我是说他叫什么名字?它位于哪些文件夹中?你在看第三个链接吗?打开此链接并查看您需要的最后一个答案
  • 我已经把它(ENGLISH.pdf)放在手机内存的根目录和 /SDcard 的根目录上。我已经尝试过您的“查看按钮”,当我启动它时,应用程序会询问我最后一个应用程序我想如何运行它
【解决方案2】:

所以第一个问题是您要求另一个应用程序在您的原始资源文件夹中打开一个文件。出于安全原因,这是不可能的。接收应用程序(在本例中为 Adob​​e Reader)需要文件位于不受保护的位置。您可以将其复制到外部公共存储区域,然后在复制的文件上打开一个意图。请记住,您不应该在 UI 线程上进行复制,也不应该每次都这样做,除非文件不存在。

第二个问题是您没有为 Intent 指定 MIME 类型。确保将其设置为“application/pdf”。

private void openPDF() {
    final InputStream in = getResources().openRawResource(R.raw.english);
    new AsyncTask<Void, Void, Void> () {
        File outDir = new File(Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/.temp");
        File outFile = new File(outDir, "english.pdf");
        @Override
        protected void onPostExecute(Void result) {
            Uri uri = Uri.fromFile(outFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
        @Override
        protected Void doInBackground(Void... params) {
            if (!outFile.exists()) {
                outDir.mkdirs();
                OutputStream out;
                try {
                    out = new FileOutputStream(outFile);
                    copyPDF(in, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }.execute();

}
private void copyPDF(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2016-07-15
    • 2020-12-11
    相关资源
    最近更新 更多