简体   繁体   中英

ANDROID: How do I know when a picture I took has finished writing to the file?

Using the android camera API I take a picture using .takepicture() and in my Camera.PictureCallback function I call

FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();

this works great although I want to immediately check how many files are in the directory and pick a random picture to show. After fos.close() I check and it does not show right away that the new picture is in the directory. How can I wait for the right time to check the directory for number of files? Thanks

Use a AsyncTask for doing that.

Your writing is start inonPreExecute and writing is finished in onPostExecute.

class MyLodingAsycTask extends AsyncTask<Void, Void, Void>{




        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // start image writing
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // image write is completed
        }

        @Override
        protected Void doInBackground(Void... params) {
            /// save your image
            return null;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM