0

The main reason I'm posting here is that I'm currently stuck without a computer that can run an emulator, I've been having to send the APK to my phone to test it. Even if my phone's connected to my computer, or I have a third party emulator running, it wont work. Due to this...I have no error logs.

The app is a simple password manager, and all the other functions thus far work. I was trying to add an export function, I can't get either to actually write anything. I've checked other questions and various sources online, but I cannot seem to figure out what could be causing it. When the method is called, it simply doesn't do anything as far as I can tell. I apologize if I'm missing something, or if there was indeed another question with the same issue. I couldn't find anything missing.

Here is the method I'm using;

EDIT: The code has been updated to reflect a better method of requesting runtime permissions, which was suggested here. This ultimately is what fixed the application.

   //Method017: Exports the account info to a .txt file.
    public void exportData() throws IOException {

        //Opens dialog to request permission.
        ActivityCompat.requestPermissions(Main.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

    //Method to handle result of permission request.
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    //Attempt to write a file to the Download folder.
                    String content = "hello world";
                    File file;
                    FileOutputStream outputStream;
                    try {
                        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache");

                        outputStream = new FileOutputStream(file);
                        outputStream.write(content.getBytes());
                        outputStream.close();

                        //According to an online source, this is necessary to make the file viewable on the device.
                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        sendBroadcast(intent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(Main.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

And my manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.brand.psync">

    <application

        android:allowBackup="true"
        android:icon="@drawable/psynclogo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:screenOrientation="portrait">
        <activity android:name=".Main">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN"
                    android:screenOrientation="portrait"    />
                <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                <category
                    android:name="android.intent.category.LAUNCHER"
                    android:screenOrientation="portrait" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Sorry about the lack of error log...but if I had that, I likely wouldn't need to post here.

  • 1
    what is the target sdk version you have mentioned in build.gradle file. If its above 22 the you have to ask WRITE_EXTERNAL_STORAGE permission at run time – Preethi Rao Oct 01 '17 at 10:42
  • Min is 15 but target is 26, and I'm testing it on 24...That could very well be it, I'll look into it. –  Oct 01 '17 at 10:44
  • 1
    If you cannot provide the stack trace, use Toast to display `e.getMessage()` in the catch block and post the error message. – Nabin Bhandari Oct 01 '17 at 10:57
  • You need request permission 'WRITE_EXTERNAL_STORAGE' not 'new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}'. – Dungnbhut Oct 02 '17 at 01:55
  • @Dungnbhut, attempted to make suggested change, got an error. http://prntscr.com/gs4p7j –  Oct 02 '17 at 02:23
  • Use ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); – Adolf Dsilva Oct 02 '17 at 02:32
  • @Audi, changed it to that already when Dungnbhut pointed out I had read not write. Still crashes. The code above has been updated for awhile now. –  Oct 02 '17 at 03:26

1 Answers1

0

I have try your code and it working.

public class SaveFileSampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView lblBackground = new TextView(this);
        lblBackground.setBackgroundColor(Color.WHITE);
        setContentView(lblBackground);

        ActivityCompat.requestPermissions(SaveFileSampleActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    //Attempt to write a file to the Download folder.
                    String content = "hello world";
                    File file;
                    FileOutputStream outputStream;
                    try {
                        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache");

                        outputStream = new FileOutputStream(file);
                        outputStream.write(content.getBytes());
                        outputStream.close();

                        //According to an online source, this is necessary to make the file viewable on the device.
                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        sendBroadcast(intent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Toast.makeText(SaveFileSampleActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

And mainifest

<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=".SaveFileSampleActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

It's work and result: enter image description here

You can review your code. I hope it can help you!

Dungnbhut
  • 176
  • 5
  • I'm confused, it looks like all you changed was removing the permission from the manifest? Only other difference I see is that I use "extends AppCompatActivity {" instead of "extends Activity {" What version of android did you test it on? –  Oct 02 '17 at 05:02
  • I'm running on emulator nexus 5 create by android studio. – Dungnbhut Oct 02 '17 at 07:15
  • Your device not created file? or not init dialog request permission? – Dungnbhut Oct 02 '17 at 07:16
  • It seems to crash when it tries to initiate the request, the dialog never comes up, it just crashes. –  Oct 02 '17 at 07:17
  • Where you call request permission? Maybe you need add log first method call request permission. If log not show, I think your app has crashed before call permission. – Dungnbhut Oct 02 '17 at 07:25
  • I have it request permission when I hit a button. The app crashes when I hit the button. I'll try to narrow down where it crashes when I get back to my computer. –  Oct 02 '17 at 07:27
  • Please feedback your result to me. – Dungnbhut Oct 02 '17 at 07:36
  • Jesus christ. It works. I just deleted something by mistake when editing my code. –  Oct 02 '17 at 08:31
  • It's great to help you! – Dungnbhut Oct 02 '17 at 15:11