-2

I am looking to do the following in andriod studio

  1. User will open his mail client and click preview on a PDF. It opens the file in adobe reader.

  2. The user makes comments in adobe and when done press share

  3. My andriod app is shown and the users selects my app

  4. My andriod app gets the pdf and saves it externally to my server.

I have part 1,2,3 so far but not 4,5. Its my understanding that you can not access applications storage, but a poster posted this, but I am unsure on how to use that?

  • Well, besides the fact you can't know if the end-user has adobe's reader on their phone, you'll have to do some research into some libraries that take your pdf file and save it to a server. There are multiple ways of doing that with HTTP/1.1 ... I would suggest looking into rolling out a simple API that takes in a request and stores the object (also allowing you to keep track of it in a database). – markbratanov May 30 '15 at 04:39
  • For faster development, look into www.parse.com / www.firebase.com for saving objects remotely, for extra credit study http://en.wikipedia.org/wiki/Representational_state_transfer – markbratanov May 30 '15 at 04:40
  • Yes, this app is for my team internally so adobe will be on everyone's phone – Mr Castrovinci May 30 '15 at 18:10
  • Apologies, didn't know that out of context. However, in order to save a file on your server I would definitely look into building a restful api for that simple put call. – markbratanov May 30 '15 at 18:12
  • Sorry I hit the enter button..... I quest what I was looking for is an example on how to get the already opened adobe file and bring it into my app. From there I got the file saving part because I can just save it to the phone and grab it to go to my server – Mr Castrovinci May 30 '15 at 18:12
  • Never mind. I just reread your question, so what is the problem you need solved? Accessing the PDF once a user hits share that allows them to save to server? – markbratanov May 30 '15 at 18:16
  • I used php for the save to server and database parts. But still need to know how to get the abode part – Mr Castrovinci May 30 '15 at 18:17
  • @markbratanov yes Exactly – Mr Castrovinci May 30 '15 at 18:19
  • Does the below code not work? – markbratanov May 30 '15 at 19:19

2 Answers2

0

In order to get the PDF you'll need to create an activity for this use case that listens for those "sharing" intents.

ShareActivity.java

void onCreate (Bundle savedInstanceState) {

    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("application/pdf".equals(type)) {
            handlePDF(intent);
        } 
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("application/pdf")) {
           // Handle multiple pdfs being sent
        }
    } else {
        // Handle other intents, such as being started from the home screen
    }
}


void handlePDF(Intent intent) {
    Uri pdfUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (pdfUri != null) {

        // TODO: Use your server-side here to save. 

    }
}

And then add this to your AndroidManifest.xml so Android knows what activity to get when they select your application to share to:

<activity android:name=".ui.ShareActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/pdf" />
    </intent-filter>
</activity>
markbratanov
  • 878
  • 3
  • 17
  • 39
  • Ok This part seems to work but saving it I thought I was clear, but I'm lost on that part too. How do I pass to php file to upload to server? – Mr Castrovinci May 31 '15 at 19:51
  • Can you post what you are doing to save the PDF? It's difficult to know how to guide you. – markbratanov May 31 '15 at 19:52
  • just added it. All help is greatly appriciated as I know PHP well but Java is all new to me – Mr Castrovinci May 31 '15 at 19:59
  • Does your server side code detect for a POST request and process that information? Is your server side code php, asp, java? Can you post that? No need to post the URL, just the code which is suppose to receive the PDF and process it. – markbratanov May 31 '15 at 20:50
  • not sure why you are using `fopen` to save the POST file to your server... At the same time, does this HAVE to be on your server? There are 3rd party alternatives like Google Drive, Google Cloud Storage and Amazon SS3 that have libraries to make saving files remotely much easier and way more secure. If not than I would suggest looking at your PHP first... – markbratanov May 31 '15 at 21:06
  • That was just a quick example I grabbed, but yes I will need to post it to my server. I have other items I need to do in the php. the main thing I need is how to post the pdf to the php page using your code above. I can edit the php for any variables – Mr Castrovinci May 31 '15 at 21:40
  • Any thoughts? Thanks – Mr Castrovinci Jun 01 '15 at 02:12
0
public class savepdf extends ActionBarActivity {
    static final int REQUEST_IMAGE_OPEN = 1;
    private static final int WRITE_REQUEST_CODE = 43;
    private Uri mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_savepdf);

        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("application/pdf".equals(type)) {
                handlePDF(intent);
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("application/pdf")) {
                // Handle multiple pdfs being sent
            }
        } else {
            // Handle other intents, such as being started from the home screen
        }
    }

    void handlePDF(Intent intent) {
        Uri pdfUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (pdfUri != null) {
            //savefile(pdfUri);

            String sourceFilename= pdfUri.getPath();
            String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath()+File.separatorChar+"abc.pdf";

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;

            try {
                bis = new BufferedInputStream(new FileInputStream(sourceFilename));
                bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
                byte[] buf = new byte[1024];
                bis.read(buf);
                do {
                    bos.write(buf);
                } while(bis.read(buf) != -1);
            } catch (IOException e) {

            } finally {
                try {
                    if (bis != null) bis.close();
                    if (bos != null) bos.close();
                } catch (IOException e) {

                }
            }















            // TODO: Use your server-side here to save.

        }







       }