public class ThumbnailFragment extends Fragment {
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1000;
private List<ImageGroup> imageGroupList;
private RecyclerView recyclerViewImages;
private ThumbnailPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public ThumbnailFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_thumbnail, container, false);
layoutManager = new LinearLayoutManager(getContext());
adapter = new ThumbnailPageAdapter();
recyclerViewImages = (RecyclerView) view.findViewById(R.id.recyclerViewImages);
recyclerViewImages.setLayoutManager(layoutManager);
recyclerViewImages.setAdapter(adapter);
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
// if we are in activity
/* ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);*/
// if we are in fragment
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
// if we have permission we can run our permission related task
ListFiles();
}
return view;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// permission-related task you need to do.
// this will run just after granting permission
// not always
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ListFiles();
}
});
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void ListFiles()
{
final Path path = new Path();
final Search search = new Search();
final AddFileHelper addFileHelper = new AddFileHelper(adapter);
search.setOnListFilesListener(new Search.OnListFilesListener() {
@Override
public void onNewFileFound(File file) {
addFileHelper.addNewFile(file);
}
@Override
public void onListCompleted(List<File> result) {
//addFileHelper.close();
}
});
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
search.listFiles(path.getDCIMFile());
}
});
thread.start();
}
}
References
https://developer.android.com/training/permissions/requesting.html