您好我开发了一个应用程序,我希望从存储中显示所有照片的文件夹。我为它实现了代码,但问题是,当创建活动时,它显示黑色空白屏幕5秒,然后显示所有文件夹。
这是我的代码:
public class ImageWithFolder extends AppCompatActivity implements AdapterView.OnItemClickListener {
List<GridViewItem> gridItems = new ArrayList<GridViewItem>();
String redirect;
GridView gridView;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagefolder);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
assert ab != null;
ab.setDisplayHomeAsUpEnabled(true);
Intent ii = getIntent();
redirect = ii.getStringExtra("next_activity");
gridView = (GridView) findViewById(R.id.gridView);
setGridAdapter();
}
private void setGridAdapter() {
createGridItems(Environment.getExternalStorageDirectory().getAbsolutePath());
MyGridAdapter adapter = new MyGridAdapter(this, gridItems);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
private void createGridItems(String directoryPath) {
File[] files = new File(directoryPath).listFiles(new ImageFileFilter());
for (File file : files) {
if (file.isDirectory()) {
if (file.listFiles((new DirFilter())).length > 0) {
createGridItems(file.getAbsolutePath());
}
if (file.listFiles((new ImageFilter())).length > 0) {
gridItems.add(new GridViewItem(file.getAbsolutePath(), true, null));
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean isImageFile(String filePath) {
if (filePath.endsWith(".jpg") || filePath.endsWith(".png") || filePath.endsWith(".jpeg")) {
return true;
}
return false;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (redirect.equalsIgnoreCase("1")) {
Intent ii = new Intent(ImageWithFolder.this, SelectPhotos.class);
ii.putExtra("path", gridItems.get(i).getPath());
startActivity(ii);
} else if (redirect.equalsIgnoreCase("2")) {
Intent ii = new Intent(ImageWithFolder.this, AlbumSelectPhotos.class);
ii.putExtra("path", gridItems.get(i).getPath());
startActivity(ii);
}
}
private class ImageFileFilter implements FileFilter {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else if (isImageFile(file.getAbsolutePath())) {
return true;
}
return false;
}
}
private class ImageFilter implements FileFilter {
@Override
public boolean accept(File file) {
if (isImageFile(file.getAbsolutePath())) {
return true;
}
return false;
}
}
private class DirFilter implements FileFilter {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return false;
}
}
}
我没有得到代码出错的地方。
任何人都可以在第一时间出现关于黑屏的解决方案。
提前致谢。
答案 0 :(得分:1)
将您的读取文件方法放在异步..
Call Aysnc:
new LoadData().execute()
public class LoadData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
createGridItems(Environment.getExternalStorageDirectory().getAbsolutePath());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MyGridAdapter adapter = new MyGridAdapter(this, gridItems);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
}
如果您对RxJava感到满意,那么请使用Observal,这是最好的解决方案。