0

I am having one heck of a time with this new permission request in Android 6. My app worked fine, and then in the last two days it is force closing all the time. If I change the target SDK level to 22, it works fine, but of course Play Store wont let you downgrade the SDK.

I need these permissions.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.android.vending.BILLING" />

After this I am not sure how to work the requesting, or new way of implementing the permissions part so the errors will stop. I have been working on this for hours and have googled and search endlessly. While alot of examples out there all tell me to do the same thing, I just cannot seem to get it write in Android Studio. Any help would be greatly appreciated.

Here is part of the code that was giving me issues.

public void exportSkin(View arg0) throws IOException {


    if (type.equals("popular")) {
        AssetManager assetManager = getAssets();
        InputStream is = assetManager.open("skins/" + skinname + ".png");

        // create a File object for the parent directory
        File wallpaperDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/SkinYourself/");
        // have the object build the directory structure, if needed.
        //noinspection ResultOfMethodCallIgnored
        wallpaperDirectory.mkdirs();
        // create a File object for the output file


        File out = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/SkinYourself/", "" + PopChar.character_name + ".png");
        byte[] buffer = new byte[1024];
        FileOutputStream fos = new FileOutputStream(out);
        int read;

        while ((read = is.read(buffer, 0, 1024)) >= 0) {
            fos.write(buffer, 0, read);
        }

        fos.flush();
        fos.close();
        is.close();


    }
    if (type.equals("create")) {
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/SkinYourself");
        myDir.mkdirs();


        String fname = "SkinYourself" + System.currentTimeMillis() / 1000 + ".png";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            b.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    */
    Toast.makeText(MainActivity.this,
            "Your skin has been saved to the SkinYourself folder check the info button on how to use it", Toast.LENGTH_LONG)
            .show();

    displayInterstitial();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    } else {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
    }
}
JMiller
  • 99
  • 11
  • "Here is part of the code that was giving me issues" -- please explain the issues. For example, if this code "is force closing all the time", use LogCat to examine the Java stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the Java stack trace, edit your question and post it as part of explaining your issues. – CommonsWare May 04 '16 at 21:48
  • 1
    This will help http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card – varunkr May 04 '16 at 21:48
  • The READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE need permissions as they are considered dangerous. I am just not sure how to implement Android M new permission handling. If i Can just do it all at the start of the activity, or do I need to do it for every function that requests read or write permissions? – JMiller May 04 '16 at 23:38
  • 1
    Yes, you can request all permissions at the start of the Activity. Be sure to check only if not yet granted – a_m May 05 '16 at 00:10
  • Thank you. This is helpful, and the code and examples provided work. I am just getting stuck on what part of my java activity I put the code. Anywhere I put it there seems to be a problem. – JMiller May 05 '16 at 04:27
  • Possible duplicate of [Android 6.0 Marshmallow. Cannot write to SD Card](http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card) – Siarhei Apr 19 '17 at 20:18

0 Answers0