Domanda

Android beginner, I am trying, for my traineeship, to create an Android app to open and read a PDF file. Then, the goal is to be able to email them and sign them digitally. For the moment, I'm just looking to open the file by pressing a button. Now my Application is Launched! I see my pretty 'Open' button, I click it and I get :

Can not open URL

Well... I tried some stuff around uri, I try to understand with: http://developer.android.com/reference/android/net/Uri.html but without success...

Have you got any ideas for help me?(my app use has something!)

here are my codes:

manifest

<?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

thanks!

È stato utile?

Soluzione

for sending PDF file from email look here Android Intent: Send an email with attachment

this Q help you to understand 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)

The general structure looks like :

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

Altri suggerimenti

So the first problem is that you're asking another app to open a file in your raw resources folder. For security reasons, this is not possible. The receiving app (Adobe Reader in this case) needs the file to be in a place that's not protected. You can copy it to the external public storage area and then open an intent on the copied file. Keep in mind that you should not do the copying on the UI thread and you shouldn't do it every time, only if the file doesn't exist.

The second problem is that you haven't specified a MIME type for the Intent. Make sure you set it to "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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top