重命名从intent中检索的文件

时间:2017-09-01 11:38:48

标签: android android-intent

我想在加密和上传之前重命名我的android中的文件(我的应用程序加密并上传文件)

权限已写入我的清单文件中 所以我从意图中检索文件,我尝试重命名它,但我一直得到一个假,这是我的代码: 有人可以帮忙吗?谢谢

public void renameFilebeforeUpload(final Intent data)
{
    new AlertDialog.Builder(getActivity())
            .setMessage("Voulez vous renommer votre fichier?")
            .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String name =  data.getData().getPath();
                    BsSdkLog.d("name of the file: "+name);
                    BsSdkLog.d("what is this?: "+data.getData().toString());
                    File sdcard = Environment.getExternalStorageDirectory();
                    File savesFile = new File(sdcard,data.getData().getPath());
                    File newFile = new File(sdcard,"hello");
                    Boolean b=savesFile.renameTo(newFile);
                    BsSdkLog.d("file renamed"+b);
                }
            })
            .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mItemManager.encryptAndUploadFile(data.getData(), mHomeViewModel.getPath(),true);
                }
            })
            .show();
}


@Override
public void onActivityResult(int requestCode, int resultCode,final  Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case FILE_PICKER:
            if (resultCode == Activity.RESULT_OK) {
               BsSdkLog.d("result is ok");
               renameFilebeforeUpload(data);

            }
            break;
        case FILE_PICKER2:
            if (resultCode == Activity.RESULT_OK) {
                mItemManager.encryptAndUploadFile(data.getData(), mHomeViewModel.getPath(),false);
            }
            break;

    }
}


public void pickUploadFile(boolean pickfile) {
    if (StringFormatter.isSharePath(mHomeViewModel.getPath())) {
        Snackbar.make(getView(), "Impossible dans un dossier partagé", Snackbar.LENGTH_LONG)
                .show();
        return;
    }

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);


    try {
        startActivityForResult(
                Intent.createChooser(intent, "Choisissez un fichier"),
                HomeFragment.FILE_PICKER);
    } catch (android.content.ActivityNotFoundException e) {
        Crashlytics.logException(e);
        // Potentially direct the user to the Market with a Dialog
        Snackbar.make(mRootView, "Vous devez installer un gestionnaire de fichiers.",
                Snackbar.LENGTH_LONG).show();
    }

我的getPath方法:

  getPath method taken entirely from HomePoint code base
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    if (BuildConfig.DEBUG)
        CustomLog.getInstance().d(" File -" +
                "Authority: " + uri.getAuthority() +
                        ", Fragment: " + uri.getFragment() +
                        ", Port: " + uri.getPort() +
                        ", Query: " + uri.getQuery() +
                        ", Scheme: " + uri.getScheme() +
                        ", Host: " + uri.getHost() +
                        ", Segments: " + uri.getPathSegments().toString()
        );

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

1 个答案:

答案 0 :(得分:2)

ACTION_GET_CONTENT不是"文件选择器" Intent行动。用户选择一段内容,您获得的Uri应该有content方案并指向该内容。 这不是文件,您无法重命名,只能通过其Urihttps://stackoverflow.com/questions/45999477/rename-file-retrieved-from-intent)重命名此网页。

此外,data.getData().getPath()不适用于content计划。

我不知道encryptAndUploadFile()做了什么。它应该使用ContentResolveropenInputStream()来获取InputStream标识的内容Uri(适用于contentfile计划)。