How to open File from sdcard in Android

How to open File from sdcard in Android - Hello friends U-See, In the article you read in this time with the title How to open File from sdcard in Android, We have prepared this article well for you to read and take the information in it. Hopefully the contents of the post Artikel Android, Which we write you can understand. Okay, happy reading.

Title: How to open File from sdcard in Android
link : How to open File from sdcard in Android

Read also


How to open File from sdcard in Android

Just open your main XML file and paste below code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My BOOK"
        android:onClick="book"/>

</LinearLayout>


Now open your java file and paste below code:


package sel.listG; //package name

import java.io.File;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Toast;
importandroid.app.Activity;
importandroid.content.ActivityNotFoundException;
importandroid.content.Intent;

public class MainActivity extends Activity  {


      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                       
           
      }
      public void book(View v)
      {
//your pdf file path
            File file=new File("/sdcard/anna.pdf");
            if(file.exists())
            {
                  Intent i= new Intent(Intent.ACTION_VIEW);
//set pdf to doc if you want to open doc
                  i.setDataAndType(Uri.fromFile(file), "application/pdf");
                  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  try
                  {
                  startActivity(i);
                  }
                  catch(ActivityNotFoundException e)
                  {
//if pdf reader not found than open browser to download pdf reader
                        Intent i1=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader&hl=en"));
                        startActivity(i1);
                  }
            }
            else
            {
//if pdf not found on given location
                  Toast.makeText(getApplicationContext(), "anna.pdf not found", Toast.LENGTH_LONG).show();
            }
      }
}


Now run your code....


That's How to open File from sdcard in Android

That's an article How to open File from sdcard in Android This time, hopefully can benefit for you all. Well, see you in other article postings.

You are now reading the article How to open File from sdcard in Android With link address https://u-see1.blogspot.com/2017/06/how-to-open-file-from-sdcard-in-android.html

Postingan terkait:

Belum ada tanggapan untuk "How to open File from sdcard in Android"

Post a Comment