我想获取特定文件夹中存在的文件列表。但listfiles
在第一次从相机捕获图像时返回大小 0。但是,如果我从相机拍摄下一张图像,则显示的尺寸1
。我检查了那个特定的文件夹,图像存在。如果我回到以前的活动并返回相机活动,它显示正确的大小。这是我的代码:
CameraTestActivity.java:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmapImageView = new ArrayList<ImageToLoad>();
((FrameLayout) findViewById(R.id.mPreview)).addView(mPreview);
ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
targetPath = ExternalStorageDirectoryPath + "/images/";
targetDirector = new File(targetPath);
scroller = (HorizontalImageScroller) findViewById( R.id.scroller_images);
adapter = new HorizontalImageScrollerAdapter(CameraTestActivity.this, bitmapImageView);
adapter.setImageSize((int) getResources().getDimension(R.dimen.image_size));
scroller.setAdapter(adapter);
this.scroller.setOnItemClickListener(this);
_horizontalImageScrollers = new ArrayList<HorizontalImageScroller>();
_horizontalImageScrollers.add(scroller);
((ImageButton)findViewById(R.id.cancel_camera)).setOnClickListener(onButtonClick);
((ImageButton)findViewById(R.id.hide)).setOnClickListener(onButtonClick);
LoadImagefromSD();
mTakePicture = (ImageButton) findViewById(R.id.takepicture);
mTakePicture.setOnClickListener(mTakePictureAction);
}
相机的 onClickEvent:
/// Handles when mTakePicture is clicked
private OnClickListener mTakePictureAction = new OnClickListener()
{
@Override
public void onClick(View v)
{
if (mCamera != null)
{
new LongOperation().execute("");
}
}
};
异步任务:
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params)
{
mCamera.takePicture();
return "Executed";
}
@Override
protected void onPostExecute(String result)
{
pDialog.dismiss();
LoadImagefromSD();
}
@Override
protected void onPreExecute()
{
pDialog = new ProgressDialog(CameraTestActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onProgressUpdate(Void... values)
{
}
}
LoadImagefromSdCard():
private void LoadImagefromSD()
{
if (targetDirector.exists())
{
bitmapImageView.clear();
File[] files = targetDirector.listFiles();
Toast.makeText(getApplicationContext(), String.valueOf("list files "+targetDirector.listFiles().length), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), String.valueOf("files length"+files.length), Toast.LENGTH_LONG).show();
for (File file : files)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
Drawable d = new BitmapDrawable(getResources(), bmp);
ImageToLoad iLoad = new ToasterToLoadDrawableResource(d, file.getName());
bitmapImageView.add(iLoad);
}
Toast.makeText(getApplicationContext(), String.valueOf("list size"+bitmapImageView.size()), Toast.LENGTH_LONG).show();
if (bitmapImageView.isEmpty())
{
btnOK.setEnabled(false);
}
else
{
btnOK.setEnabled(true);
}
adapter.notifyDataSetChanged();
childCount = scroller.getChildCount();
scroller.setSelection(bitmapImageView.size()-1);
}
else
{
targetDirector.mkdirs();
}
}