0

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;
    }
}

活动主.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

谢谢!

4

2 回答 2

1

从电子邮件发送 PDF 文件看这里Android Intent: Send an email with attachment

这个Q帮助你理解uri URI、URL和URN有什么区别?

在这里您可以找到如何创建一个浏览按钮,当您单击该按钮时,它将打开 SDCARD,您将选择一个文件,结果您将获得所选文件的文件名和文件路径。找到你可以使用 Log.d 方法的 uri 地址(转到最后一个答案)

一般结构如下所示:

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);
于 2013-06-13T23:34:47.043 回答
0

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

第二个问题是您没有为 Intent 指定 MIME 类型。确保将其设置为“应用程序/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);
    }
}
于 2013-06-14T17:49:18.937 回答