ListFiles第一次没有正确获取文件夹文件

时间:2013-05-18 02:24:58

标签: android android-camera android-sdcard

我想获取特定文件夹中存在的文件列表。但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("");
  }

}

};

的AsyncTask:

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();
    }
  }

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我正在给那些可能像我一样得到同样问题的人。实际上问题不在于列出目录中的文件。它工作正常。但是当我拍照时,listfiles在图片字节写入sdcard文件夹之前获取文件列表。所以我通过在字节写入文件夹之后获取​​文件来改变编码。我只是调用文件加载方法,如CameraTestActivity.LoadImagefromSD();以下是编码。

/// Handles data for jpeg picture
    private PictureCallback jpegCallback = new PictureCallback()
    {
      @Override
      public void onPictureTaken(byte[] data, Camera camera) 
      {
        //immediately start the preview again
        ///@see http://developer.android.com/reference/android/hardware/Camera.html#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29
        if (mCamera != null)
          mCamera.startPreview();

        FileOutputStream outStream = null;
        try {
          // Write to SD Card
              outStream = new FileOutputStream(String.format(Environment.getExternalStorageDirectory().getAbsolutePath()+"/images/%d.jpg",
            System.currentTimeMillis()) );
              // outStream = new FileOutputStream(String.format("/data/data/com.mattikariluoma.cameratest/files/images/%d.jpg",
          outStream.write(data);
          outStream.close();
          Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
          CameraTestActivity.LoadImagefromSD();

        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
        }

        Log.d(TAG, "onPictureTaken - jpeg");
      }
    };

希望它会对某人有所帮助。