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
谢谢!