将照片离线存储在缓存中

问题描述 投票:-1回答:1

是否可以在离线状态下将照片存储在android中以在设备上本地缓存,并且每当您访问Internet时,它将在缓存中上传所有排队的照片?

private File createImageFile() throws IOException {
// Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                Log.e( TAG,"created image");
            } catch (IOException ex) {

                Log.e( TAG,"takePictureIntent: IOException: "+ex.getMessage(),ex);

                // Error occurred while creating the File...
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Log.e( TAG,"Not null ");
                photoURI = FileProvider.getUriForFile(getActivity(),
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                Log.e( photoURI.toString(),"url");
                startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        }
    }
android firebase android-camera
1个回答
0
投票

外部存储路径是放置它的正确位置。您可以在其中保存图像的位置创建一个文件夹,这是一种很好的方法,并且可以确保仅删除要删除的文件。这是有关如何删除文件夹中所有文件的示例。您可以将其更改为在上传后一次删除一个文件,也可以在删除文件之前添加上传代码。我不知道这段代码是否可以编译,但这与该代码非常接近:

   String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";

    AssetManager mgr = getAssets();

    try {

        String list[] = mgr.list(path);
        Log.e("FILES", String.valueOf(list.length));

        if (list != null) {
            for (int i=0; i < list.length; ++i)
                {

                   // Here you could upload the file at path +"/"+ list[i]

                   // If successful, then delete the file
                   File file = new File(path +"/"+ list[i]);
                   file.delete();
                   // Sometimes, deletes don't work the first time, so try it different ways
                   if(file.exists()){
                      file.getCanonicalFile().delete();
                      // If file still exists, try a third way!
                      if(file.exists()){
                          getApplicationContext().deleteFile(file.getName());
                   }
                }

        }

    } catch (IOException e) {
        Log.v("List error:", "can't list" + path);
    }

不要忘记,您需要在AndroidManifest.xml文件中包括写入外部存储的权限。

© www.soinside.com 2019 - 2024. All rights reserved.