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
谢谢!
答案 0 :(得分:1)
从电子邮件发送PDF文件,请点击此处Android Intent: Send an email with attachment
这个Q帮助你了解uri What is the difference between a URI, a URL and a URN?
一般结构如下:
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);
答案 1 :(得分:0)
所以第一个问题是你要求另一个应用程序在原始资源文件夹中打开一个文件。出于安全原因,这是不可能的。接收应用程序(本例中为Adobe 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);
}
}