创建一个新文件夹,用户可以在其中指定文件夹的名称

时间:2018-03-23 20:59:34

标签: java android

我正在尝试创建一个应用程序,用户可以在其中创建新文件夹并将数据保存到其中。 当用户单击“添加文件夹”时,会出现一个警告对话框,其中包含一个文本字段,用于从用户处获取该文件夹的名称。
这是我的代码

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());

    final EditText input = new EditText(getActivity());
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    builder.setView(input);

    LayoutInflater inflater=getActivity().getLayoutInflater();

    View view=inflater.inflate(R.layout.folder_dialog,null);

    builder.setView(view)
            .setTitle("Create New Folder")
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                }
            })
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    String value = input.getText().toString();
                    File ABC=new File("/Simply-File/Pdf",value);
                    if(!ABC.exists())
                    {
                        ABC.mkdir();
                        Toast.makeText(getActivity(),"Folder Created!",Toast.LENGTH_LONG).show();
                    }


                }
            });


    return builder.create();
}

我已经创建了一个父目录,看起来像这样

   private void writeExternalStorage()
{
    String state;
    state= Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state))
    {

        File root= Environment.getExternalStorageDirectory();
        File Dir=new File(root.getAbsolutePath(),"/Simply-File/Pdf");
        if(!Dir.exists())
        {
            Dir.mkdirs();
        }

    }
    else
    {
        Toast.makeText(getApplicationContext(),"SD card not detected!",Toast.LENGTH_LONG).show();
    }
}

我还要求许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  

folder_dialog.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
    android:hint="Folder Name"
    android:id="@+id/create_folder"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

1 个答案:

答案 0 :(得分:0)

问题在于EditText。当用户在对话框上单击“确定”时,您正在创建新的EditText并将其中的文本分配给value变量。此value将始终为空字符串,因为此EditText不是Dialog中显示的字符串。您从布局文件中展开视图,然后在builder中设置该视图,EditText中显示的Dialog将是在EditText中声明的视图XML文件。

由于您在android:id="@+id/create_folder"布局文件中声明了folder_dialog.xml EditText,因此您应该按ID获取该视图,而不是以编程方式创建新的final EditText input = new EditText(getActivity()); builder.setView(input); View view=inflater.inflate(R.layout.folder_dialog,null); builder.setView(view) (...)

这部分代码:

View view = inflater.inflate(R.layout.dialog, null);
final EditText input = view.findViewById(R.id.create_folder);
builder.setView(view) (...)

应改为:

    Mat refDescriptors = new Mat();
    FeatureDetector orbFeatureDetector = FeatureDetector.create(FeatureDetector.ORB);
    orbFeatureDetector.detect(grayMat, refKeypoints);
    Mat Out = new Mat();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    descriptorExtractor.compute(grayMat, refKeypoints, refDescriptors);
    Features2d.drawKeypoints(grayMat, refKeypoints, Out,new Scalar(2,254,255),Features2d.DRAW_RICH_KEYPOINTS);
    //System.out.println(refKeypoints);
    Utils.matToBitmap(Out,grayBitmap);
    imageView.setImageBitmap(grayBitmap);
    JsonObject obj = new JsonObject();

    if(refKeypoints.isContinuous()){
        int cols = refKeypoints.cols();
        int rows = refKeypoints.rows();
        int elemSize = (int) refKeypoints.elemSize();

        byte[] data = new byte[cols * rows * elemSize];

        refKeypoints.get(0, 0, data);

        obj.addProperty("rows", refKeypoints.rows());
        obj.addProperty("cols", refKeypoints.cols());
        obj.addProperty("type", refKeypoints.type());

        // We cannot set binary data to a json object, so:
        // Encoding data byte array to Base64.
        String dataString = new String(Base64.encode(data, Base64.DEFAULT));

        obj.addProperty("data", dataString);

        Gson gson = new Gson();
        String json = gson.toJson(obj);

    } else {

    }