thpitsch
thpitsch

Reputation: 2036

Android Expansion File Downloder - Write Permission?

My Android Expansion File Downloder is not working. I found out, that the problem occurs in the original DownloadThread.java I have verified that that external media is mounted.

The passed data is correct: byte[] data contains bytesRead = 4096 Bytes. These are checked and were correctly read from the expected obb file. Also state.filename is absolutely correct. state.mStream = null, therefore a FileOutputStream must be opened first.

new FileOutputStream(state.mFilename, true) produces an IOException

private void writeDataToDestination(State state, byte[] data, int bytesRead)
        throws StopRequest {
    for (;;) {
        try {
            if (state.mStream == null) {
                state.mStream = new FileOutputStream(state.mFilename, true);
            }
            state.mStream.write(data, 0, bytesRead);
            // we close after every write --- this may be too inefficient
            closeDestination(state);
            return;
        } catch (IOException ex) {
            if (!Helpers.isExternalMediaMounted()) {
                throw new StopRequest(DownloaderService.STATUS_DEVICE_NOT_FOUND_ERROR,
                        "external media not mounted while writing destination file");
            }

The WRITE_EXTERNAL_STORAGE permission should be given: AndroidManifest.xml:

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

...
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name_short"
        android:supportsRtl="true"
        android:requestLegacyExternalStorage="true"
        android:preserveLegacyExternalStorage="true"
        android:theme="@style/AppTheme">
...

And my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    useLibrary 'org.apache.http.legacy'

    signingConfigs {
        Signed {
            keyAlias ...
            keyPassword ...
            storeFile file(...)
            storePassword ...
        }
    }

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 30
        ndk {
            debugSymbolLevel 'FULL'
        }
    }
...

I'm totally stuck and can't continue. What can I do?

Upvotes: 1

Views: 104

Answers (2)

thpitsch
thpitsch

Reputation: 2036

G-o-o-g-l-e, if you yourself can't save the obb files to the obb directory, then I don't need to. I have now found another directory for it that can be written to by my app.

Upvotes: 0

Pouria Hemi
Pouria Hemi

Reputation: 735

you should requested permission

 private void checkWriteStoragePermission()
{
    ArrayList<String> permissions = new ArrayList<>();
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    {
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    {
        permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    if (permissions.size() > 0)
    {
        ActivityCompat.requestPermissions(this, permissions.toArray(new String[permissions.size()]), Constants.WRITE_EXTERNAL_STORAGE());
    }
    
}

and use REQUEST_INSTALL_PACKAGES permission it can access Android/obb directory.

Upvotes: 0

Related Questions