目录不是在Android中创建的

时间:2017-07-26 19:33:54

标签: android directory

以下代码既不创建目录也不将照片存储在必须创建的目录中。我不知道bug来自哪里,我正在搜索互联网,但我找不到有用的信息。你看到有什么错吗?

       mImageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            //Creamos la carpeta.
            File imageFolder = new File(Environment.getExternalStorageDirectory() + "/LibretaPolicial");
            if(!imageFolder.exists()){
                imageFolder.mkdirs();
                Toast.makeText(BorrarActivity.this, "Creado", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(BorrarActivity.this, "NOOOOO creado", Toast.LENGTH_SHORT).show();
            }



            File image = new File(imageFolder, numeroAleatorio+".jpg");
            Uri uriSavedImage = Uri.fromFile(image);

            //Le decimos al Intent que queremos guardar la imagen.
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            //Lamzamos la aplicación de la camara
            startActivityForResult(cameraIntent, 1);
        }
    });


}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Comprovamos que la foto se a realizado
    if (requestCode == 1 && resultCode == RESULT_OK) { //requestCode: 1 requestCode : 0
        //Creamos un bitmap con la imagen recientemente
        //almacenada en la memoria
        Bitmap bMap = BitmapFactory.decodeFile(
                getExternalStorageDirectory()+
                        "/LibretaPolicial/"+numeroAleatorio+".jpg");
        //Añadimos el bitmap al imageView para
        //mostrarlo por pantalla
        mImageView.setImageBitmap(bMap);
    }
}

我相信AndroidManifest中的权限并没有向我显示任何错误,只是不要创建目录或将照片存储在应该创建的目录中。

   <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activities.SplashActivity">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN"
                    android:screenOrientation="portrait"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Activities.MainActivity"
            android:screenOrientation="portrait">

        </activity>
        <activity
            android:name=".Activities.BorrarActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ModificarActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.BorrarPlacasActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ModificarPlacaActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ClaveActivity"
            android:screenOrientation="portrait">
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
    </uses-permission>

</manifest>

2 个答案:

答案 0 :(得分:0)

尝试使用:

File imageFolder = new File(Environment.getExternalStorageDirectory(), "LibretaPolicial");

答案 1 :(得分:0)

我只是想知道您是如何使用app.exe前缀在"%~dp0..\Test2\app.exe" 中拨打getExternalStorageDirectory()的?

无论如何,我确实写了一个可以帮助你的新代码

  
      
  • 在创建目录
  • 之前检查onActivityResult的状态   
  • 我使用Environment代替Environment.getExternalStorageState()以确保安全(使用/可能导致错误)
  •   
  • 我创建了一个临时文件,以便在此之后加快加载速度
  •   

这是代码

File.separator

然后在/

public static final int REQUEST_TAKE_PHOTO = 1;
private File photoCapturedFile;
public void openCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = System.currentTimeMillis() + ""; // You can use any name
    String fileName = "JPEG_" + timeStamp;

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        photoCapturedFile = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),
                fileName + ".jpg");
    } else {
        String dir = Environment.getExternalStorageDirectory() + File.separator + "myDirectory";
        //create folder
        File folder = new File(dir); //folder name
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //create file
        photoCapturedFile = new File(dir, fileName + ".jpg");
    }

    Uri temUri = Uri.fromFile(photoCapturedFile);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, temUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
    }
}