2017-04-25 11 views
-1

グリッドビュー内の特定のフォルダからの画像を表示するフラグメントを使用しています... Lollipop &プレLollipopで、それはうまく動作しますが、MまたはM +デバイスでは、「取得しようとしましたここでは同様にgetCount()メソッドでは、私のアダプタでのNULLの配列」の長さ...getCount()でNull配列の長さを取得しようとしました。

私が間違っているつもりですどこ私にはわからない...

は断片ダウンロードのために私のコードです -

public class Downloaded extends Fragment{ 



    // Declare variables 
    private String[] FilePathStrings; 
    private String[] FileNameStrings; 
    private File[] listFile; 
    GridView grid; 
    GridViewAdapter adapter; 
    File file; 
    TextView empty; 

@Override 
public void onActivityCreated(Bundle 
savedInstanceState) { 
super.onActivityCreated(savedInstanceState); 
    getActivity().setTitle("Saved"); 
} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    { 
     //Inflating the Layout 
     View rootView = inflater.inflate(R.layout.gridview_main, container, false); 


     // Check for SD Card 
     if (!Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG) 
        .show(); 
     } else { 
      // Locate the image folder in your SD Card 
      file = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "Sample"); 
      // Create a new folder if no folder named SDImageTutorial exist 
      file.mkdirs(); 
     } 

     if (file.isDirectory()) { 
      listFile = file.listFiles(); 
      // Create a String array for FilePathStrings 
      FilePathStrings = new String[listFile.length]; 
      // Create a String array for FileNameStrings 
      FileNameStrings = new String[listFile.length]; 

      for (int i = 0; i < listFile.length; i++) { 
       // Get the path of the image file 
       FilePathStrings[i] = listFile[i].getAbsolutePath(); 
       // Get the name image file 
       FileNameStrings[i] = listFile[i].getName(); 
      } 
     } 


     // Locate the GridView in gridview_main.xml 
     grid = (GridView) rootView.findViewById(R.id.gridview); 

     empty = (TextView) rootView.findViewById(R.id.empty); 

     empty.setText("You haven't saved any Pic yet...!"); 

     grid.setEmptyView(empty); 

     // Pass String arrays to LazyAdapter Class 
     adapter = new GridViewAdapter(getActivity(), FilePathStrings, FileNameStrings); 

     // Set the LazyAdapter to the GridView 
     grid.setAdapter(adapter); 



     // Capture gridview item click 
     grid.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       Intent i = new Intent(getActivity(), ViewImage.class); 
       // Pass String arrays FilePathStrings 
       i.putExtra("filepath", FilePathStrings); 
       // Pass String arrays FileNameStrings 
       i.putExtra("filename", FileNameStrings); 
       // Pass click position 
       i.putExtra("position", position); 
       startActivity(i); 
      } 

     }); 
     return rootView; 
    } 

} 

ここは私のアダプターです -

public class GridViewAdapter extends BaseAdapter { 

    // Declare variables 
    private Activity activity; 
    private String[] filepath; 
    private String[] filename; 

    Context mContext; 
    private static LayoutInflater inflater = null; 

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) { 
     activity = a; 
     this.filepath = fpath; 
     filename = fname; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return this.filepath.length; 
    // Getting a Crash here! 

    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 

     if (convertView == null) 
      vi = inflater.inflate(R.layout.gridview_item, null); 

     // Locate the ImageView in gridview_item.xml 
     ImageView img = (ImageView) vi.findViewById(R.id.image); 


     // Set file name to the TextView followed by the position 
     TextView txt = (TextView) vi.findViewById(R.id.name); 

     // Decode the filepath with BitmapFactory followed by the position 


     // Set the decoded bitmap into ImageView 
     Glide.with(activity) 
     .load(filepath[position]) 
     .into(img); 

     txt.setText(filename[position]); 

     return vi; 
    } 
} 

答えて

1

file.mkdirs()の戻り値を確認してください。おそらくfalseが返され、if (file.isDirectory())が失敗し、FilePathStringsが作成されない可能性があります。

これは、アプリに外部ストレージにアクセスするための適切な権限がない場合に発生します(例:WRITE_EXTERNAL_STORAGE)。 M以上をターゲットにする場合は、実行時アクセス権を要求していることを確認してください。ただし、Mをターゲットにしていなくても、ランタイム権限モデルを使用していない「レガシー」スタイルのアプリケーションの権限を取り消すことは可能です。

関連する問題