0

this codes was from another post : Android open pdf file

this is all my codes in 1 class called openFile.class

package com.test.android;


import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import java.io.File;

public class openFile {

  String namafile ;
  public openFile(String namafile){
    this.namafile = namafile;
  }

  File file = new
  File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+   
  namafile);
  Intent target = new Intent(Intent.ACTION_VIEW);
  target.setDataAndType(Uri.fromFile(file),"application/pdf");
  target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

  Intent intent = Intent.createChooser(target, "Open File");
    try {
      startActivity(intent);
    } catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
   }

}

any suggestions will be accepted

Community
  • 1
  • 1
  • 1
    All the code you got from that answer needs to be inside a method. – Mike M. Aug 03 '16 at 04:15
  • @MikeM. I will try to put it inside a method later, thanks. – Jokurilisme Aug 03 '16 at 04:52
  • Sorry i forgot to mention my goal. I want create this class to open a PDF file after being downloaded to a smartphone. So i just created a method, put that codes inside a method and it works, now the error changed to startActivity(intent); Cannot resolve method 'startActivity(android.content.intent)' – Jokurilisme Aug 03 '16 at 05:14

1 Answers1

0

As @MikeM. suggested in the comments:

All the code you got from that answer needs to be inside a method.

divibisan
  • 11,659
  • 11
  • 40
  • 58
MindRoasterMir
  • 324
  • 1
  • 2
  • 18