2017-12-20 10 views
0

アセットからPDFファイルを開くgridviewを作成する ダウンロード・ディレクトリを使用してpdfファイルを表示しましたが、今はアセットを使用したい 主なアクティビティを変更しています断片 みんなにPDFの活動は、資産フォルダで私を助けてアセット・フォルダを使用してandroid studioのgridviewに表示する方法

サンプルコード

CustomAdaptor.java

package com.tutorials.hp.gridviewpdf; 

import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 


public class CustomAdapter extends BaseAdapter { 

    Context c; 
    ArrayList<PDFDoc> pdfDocs; 

    public CustomAdapter(Context c, ArrayList<PDFDoc> pdfDocs) { 
     this.c = c; 
     this.pdfDocs = pdfDocs; 
    } 

    @Override 
    public int getCount() { 
     return pdfDocs.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return pdfDocs.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     if(view==null) 
     { 
      //INFLATE CUSTOM LAYOUT 
      view= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false); 
     } 

     final PDFDoc pdfDoc= (PDFDoc) this.getItem(i); 

     TextView nameTxt= (TextView) view.findViewById(R.id.nameTxt); 
     ImageView img= (ImageView) view.findViewById(R.id.pdfImage); 

     //BIND DATA 
     nameTxt.setText(pdfDoc.getName()); 
     img.setImageResource(R.drawable.pdf_icon); 

     //VIEW ITEM CLICK 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       openPDFView(pdfDoc.getPath()); 
      } 
     }); 
     return view; 
    } 

    //OPEN PDF VIEW 
    private void openPDFView(String path) 
    { 
     Intent i=new Intent(c,PDF_Activity.class); 
     i.putExtra("PATH",path); 
     c.startActivity(i); 
    } 
} 

PDF_Activity.java iが

package com.tutorials.hp.gridviewpdf; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 

import com.github.barteksc.pdfviewer.PDFView; 
//import com.github.barteksc.pdfviewer.ScrollBar; 
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; 

import java.io.File; 

public class PDF_Activity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pdf); 

     //PDFVIEW SHALL DISPLAY OUR PDFS 
     PDFView pdfView= (PDFView) findViewById(R.id.pdfView); 
     //SCROLLBAR TO ENABLE SCROLLING 
//  ScrollBar scrollBar = (ScrollBar) findViewById(R.id.scrollBar); 
//  pdfView.setScrollBar(scrollBar); 
     //VERTICAL SCROLLING 
//  scrollBar.setHorizontal(false); 
     //SACRIFICE MEMORY FOR QUALITY 
     //pdfView.useBestQuality(true) 

     //UNPACK OUR DATA FROM INTENT 
     Intent i=this.getIntent(); 
     String path=i.getExtras().getString("PATH"); 

     //GET THE PDF FILE 
     File file=new File(path); 

      if(file.canRead()) 
      { 
       //LOAD IT 

       pdfView.fromFile(file).defaultPage(1).onLoad(new OnLoadCompleteListener() { 
        @Override 
        public void loadComplete(int nbPages) { 
         Toast.makeText(PDF_Activity.this, String.valueOf(nbPages), Toast.LENGTH_LONG).show(); 
        } 
       }).load(); 

      } 


    } 
} 

PDFDoc.java iがその断片を使用する

package com.tutorials.hp.gridviewpdf; 

import android.net.Uri; 

public class PDFDoc { 
    String name,path; 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 
} 

MainActivity.java代わり活性 iは資産を必要フラグメント代わりアクティビティを使用しますPDFファイルを表示するフォルダ

package com.tutorials.hp.gridviewpdf; 

import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.GridView; 

import java.io.File; 
import java.util.ArrayList; 


public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     final GridView gv= (GridView) findViewById(R.id.gv); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       gv.setAdapter(new CustomAdapter(MainActivity.this,getPDFs())); 

      } 
     }); 
    } 

    private ArrayList<PDFDoc> getPDFs() 

    { 
     ArrayList<PDFDoc> pdfDocs=new ArrayList<>(); 
     //TARGET FOLDER 
     File downloadsFolder= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

     PDFDoc pdfDoc; 

     if(downloadsFolder.exists()) 
     { 
      //GET ALL FILES IN DOWNLOAD FOLDER 
      File[] files=downloadsFolder.listFiles(); 

      //LOOP THRU THOSE FILES GETTING NAME AND URI 
      for (int i=0;i<files.length;i++) 
      { 
       File file=files[i]; 

       if(file.getPath().endsWith("pdf")) 
       { 
        pdfDoc=new PDFDoc(); 
        pdfDoc.setName(file.getName()); 
        pdfDoc.setPath(file.getAbsolutePath()); 

        pdfDocs.add(pdfDoc); 
       } 

      } 
     } 

     return pdfDocs; 
    } 


} 

レイアウト

activity_main.xml Iフラグメント代わり活性

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="com.tutorials.hp.gridviewpdf.MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

activity_pdf.xml を使用する私の代わりに活性

<?xml version="1.0" encoding="utf-8"?> 
<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="com.tutorials.hp.gridviewpdf.PDF_Activity"> 

    <com.github.barteksc.pdfviewer.PDFView 
     android:id="@+id/pdfView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@+id/scrollBar"/> 

    <com.github.barteksc.pdfviewer.ScrollBar 
     android:id="@+id/scrollBar" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 
の断片を使用します

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.tutorials.hp.gridviewpdf.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="PDF VIEWER" /> 

     <GridView 
      android:id="@+id/gv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:numColumns="2" /> 
    </LinearLayout> 


</RelativeLayout> 

model.xml

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="match_parent" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:layout_margin="10dp" 
     card_view:cardCornerRadius="5dp" 
     card_view:cardElevation="5dp" 
     android:layout_height="200dp"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:id="@+id/pdfImage" 
        android:src="@drawable/pdf_icon" 
        android:layout_width="150dp" 
        android:layout_height="wrap_content" /> 

       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textAppearance="?android:attr/textAppearanceLarge" 
         android:text="Name" 
         android:id="@+id/nameTxt" 
         android:padding="10dp" 
         android:textColor="@color/colorAccent" 
         android:textStyle="bold" 
         android:layout_alignParentLeft="true" 
         /> 

       </LinearLayout> 

       </LinearLayout> 


    </android.support.v7.widget.CardView> 
+0

以下のリンクを参照してください。 https:// stackoverflow。com/questions/17085574 /アセットファイルからPDFファイルを読み取る – Prateek218

答えて

関連する問題