0

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

1 回答 1

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

希望它会帮助某人。

于 2013-05-23T01:11:58.563 回答