将文件保存到特定文件夹并转换为zip文件

时间:2018-01-10 06:37:49

标签: android save zip directory

我必须上传文件并转换为zip文件并保存到服务器。

但在上传到服务器之前,文件应该包装在一个文件夹中然后应该转换为zip文件。

在下面的代码中,我可以直接转换为zip文件而不将其包装在文件夹中。

我使用 M-zip 库转换为zip文件。

在上传到服务器之前,能帮我将文件上传到文件夹中吗?

public void uploadMultipart() {
    //getting name for the image
    String subject = subj.getText().toString().trim();
    String description = desx.getText().toString().trim();
    String depart = spinner.getSelectedItem().toString();
    String prior = spinner2.getSelectedItem().toString();
    //getting the actual path of the image

    String path = FilePath.getPath(getActivity(), filePath);

    String filename = path.toString();
    String zipPath=null;

        // Do something on success

        String fname = filename.substring(0, filename.lastIndexOf("."));
        zipPath = fname+".zip";
        Log.d("FileNameZip",zipPath);


        ZipArchive zipArchive = new ZipArchive();
        zipArchive.zip(path, zipPath, "");


    String p = null;
    int cut = filename.lastIndexOf('/');
    p = filename.substring(0,cut+1);
    if (cut != -1) {
        Log.d("FileNameCut",String.valueOf(cut));
        filename = filename.substring(cut + 1);
        Log.d("FileName " , p);
        Log.d("FileName " , p+filename);

    }


        } catch (Exception exc) {
            Toast.makeText(getActivity(), exc.getMessage(), 
  Toast.LENGTH_SHORT).show();
        }
    }
}


//method to show file chooser
private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_Image_REQUEST);
}
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/dIRNAME");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DIRNAME/");
        wallpaperDirectory.mkdirs();
    }

    File file = new File(new File("/sdcard/DIRNAME/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.valueOf(fileName), 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
//handling the image chooser activity result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_Image_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();

        String filename = FilePath.getPath(getActivity(), filePath);

        int cut = filename.lastIndexOf('/');
        if (cut != -1) {
            filename = filename.substring(cut + 1);
        }


        filetext.setText(filename);
      //  try {
       //     bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),filePath);
     //   } catch (IOException e) {
      //      e.printStackTrace();
      //  }


    }
}

//method to get the file path from uri


//Requesting permission
private void requestStoragePermission() {
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
        return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }
    //And finally ask for the permission
    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}


//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

        //If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Displaying a toast
            Toast.makeText(getContext(), "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            //Displaying another toast if permission is not granted
            Toast.makeText(getContext(), "Oops you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}

0 个答案:

没有答案