2017-10-15 9 views
0

J'essaie de créer un fichier image sur le stockage externe pour le partager. mais en essayant le code suivant, j'ai une erreurAutorisation refusée lors de l'appel de File.createNewFile() | Android

 var icon: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.namelogo) 

     val shareIntent = Intent(Intent.ACTION_SEND) 
     shareIntent.type = "image/*" 
     val bytes = ByteArrayOutputStream() 
     icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes) 
     val path = File.separator + "temporary_file.jpg" 

     val f = File(Environment.getExternalStorageDirectory().toString(), path) 
     try { 


      if (!f.parentFile.exists()) 
       f.parentFile.mkdirs() 
      if (!f.exists()) 
       f.createNewFile() 
      f.createNewFile() 
      val fo = FileOutputStream(f) 

      fo.write(bytes.toByteArray()) 
     } catch (e: IOException) { 
      Log.e("path = ", "+ $path " + e.toString()) 
      e.printStackTrace() 
     } 

     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/IronyBalls/temporary_file.jpg")); 
     startActivity(Intent.createChooser(shareIntent, "Share Image")) 

Jusqu'à présent, j'ai trouvé des solutions que d'utiliser

if (!f.parentFile.exists()) 
     f.parentFile.mkdirs() 
if (!f.exists()) 
     f.createNewFile() 

et

pour définir l'autorisation

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

que j'ai déjà utilisé.Mais encore erreur gettig comme ci-dessous en Erreur Tab

E/path =: + /temporary_file.jpg java.io.IOException: Permission refusée

et dans l'onglet info

W/système. err: java.io.IOException: autorisation refusée

W/System.err: à java.io.UnixFileSystem.createFileE xclusively0 (Native Method)

W/System.err: à java.io.UnixFileSystem.createFileExclusively (UnixFileSystem.java:280)

W/System.err: à java.io.File.createNewFile (File.java:948) W/System.err: à com.irony.balls.MainActivity $ onCreate $ 8.onClick (MainActivity.kt: 277)

W/System.err: à android.view.View .performClick (View.java:5611)

10-15 20: 33: 17.912 29759-29759/com.irony.balls W/System.e rr: à android.view.View $ PerformClick.run (View.java:22276) 10-15 20: 33: 17.912 29759-29759/com.irony.balls W/System.err: à android.os .Handler.handleCallback (Handler.java:751) 10-15 20: 33: 17.912 29759-29759/com.irony.balls W/System.err: à android.os.Handler.dispatchMessage (Handler.java: 95) 10-15 20: 33: 17.912 29759-29759/com.irony.balls W/System.err: à android.os.Looper.loop (Looper.java:154) 10-15 20:33: 17.912 29759-29759/com.irony.balls W/System.err: à android.app.ActivityThread.main (ActivityThread.java:6195) 10-15 20: 33: 17.912 29759-29759/com.irony. balles W/System.err: à java.lang.reflect.Method.invoke (Méthode native) 10-15 20: 33: 17.912 29759-29759/com.irony.balls W/System.err: à com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:874)

10-15 20: 33: 17,912 29759-29759/com.irony.balls W/System.err: à com.android.internal.os.ZygoteInit. main (ZygoteInit.java: 764)

+0

Quelle est votre 'targetSdk'? –

+0

@ H.Brooks 'targetSdk' est 26 –

+1

Oui, alors vous aurez besoin d'autorisations d'exécution, la solution serait de changer votre' targetSdk' à 23. Comme 24 autorisations d'exécution sont nécessaires. –

Répondre