Android Cannot get access to data folder

paulgoux solved it! One year and seven months later I FINALLY got the answer! (I replied in case anyone else was struggling with this topic)

AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28"/>
    <application android:icon="@mipmap/ic_launcher" android:label="">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

Activity activity; 
Context context; 
Permission p;
String s1 ,s2 = "iuiugifuy";
String absolutePath;
public boolean overWrite = true;

public void setup(){
  size(600,600);
  p = new Permission(this,"WRITE_EXTERNAL_STORAGE");
  activity = this.getActivity(); 
  context = activity.getApplicationContext();
  absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 
  
};

public void draw(){
  background(255);
  
  
  if(s1!=null){
    fill(0);
    text(s1,20,20);
  }else{
    fill(0);
    text("no file",20,20);
  }
  
};

void mousePressed(){
  generateNoteOnSD(context,"test.txt",s2+hour()+":"+minute()+":"+second());
  readFromFile(context);
};


public void generateNoteOnSD(Context context, String FileName, String text) {
  try {
    //
      File root = new File(absolutePath, "Notes");
      if (!root.exists()) {
          root.mkdirs();
      }
      File gpxfile = new File(root, FileName);
      //set to false will overwrite the file, set to true to append
      FileWriter writer = new FileWriter(gpxfile,false);
      //use write or append
      writer.append(text);
      // use \n to add new line
      writer.append("\n");
      
      writer.flush();
      writer.close();
      println("write file");
  } catch (IOException e) {
      e.printStackTrace();
  }
};

  
private String readFromFile(Context context) {

    FileInputStream fis = null;
    try {

        fis = new FileInputStream (new File(absolutePath+"/notes/"+"test.txt"));
        InputStreamReader isr = new InputStreamReader(fis);
        // READ STRING OF UNKNOWN LENGTH
        StringBuilder sb = new StringBuilder();
        char[] inputBuffer = new char[2048];
        int l;
        // FILL BUFFER WITH DATA
        while ((l = isr.read(inputBuffer)) != -1) {
            sb.append(inputBuffer, 0, l);
            println("write data",inputBuffer, 0, l);
        }
        // CONVERT BYTES TO STRING
        s1 = sb.toString();
        
        fis.close();

    }catch (Exception e) {
      println("cannot fetch file",e);
    } finally {
        if (fis != null) {
            
            fis = null;
        }
    }
import android.os.Environment;
import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.os.Build.VERSION_CODES; 
import processing.core.PConstants;
import android.app.Activity; 

PImage img;
Activity activity; 
boolean image_saved;
boolean permissions_granted;

// Folowing file name is the folder you save your image file in.
// If it does not exist it will be created automatically
String folder_name = "MyImageFolder";

// Folowing string is the name of your image file
// Be sure to include extension.
String image_file = "MyImage.jpg";
Permission wstorage,rstorage;
void setup() {
  wstorage = new Permission(this,"WRITE_EXTERNAL_STORAGE");
  rstorage = new Permission(this,"READ_EXTERNAL_STORAGE");
  size(displayWidth, displayHeight);
  background(0, 0, 200); 
  rectMode(CENTER);
  rect(width/2, height/2, width, height/2);
  textAlign(CENTER);
  String str = "Click to save an image.";
  textSize(12);
  float twf = width/textWidth(str);
  textSize(8*twf);
  fill(0);
  text(str, width/2, height/2);
  img = get();
  //img.save(
};

void draw() {
  if (image_saved) {
    background(0, 0, 200);
    fill(255);
    text("Image saved.", width/2, height/2);
  } 
};

void mousePressed() {
  saveMyImage(folder_name, image_file);
};

void saveMyImage(String sf, String tf) { 
  try { 
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()); 
    File file = new File(absolute_path+"/"+sf); 
    //println(file);
    if (!file.exists()) { 
      boolean success = true; 
      success = file.mkdirs();
      if(success){
        save(file+"/"+tf);
        image_saved = true;
        println("File saved successfully.");
        println("Path",absolute_path,"File",file);
      }
      else {
        println("no File");
        println("Path",absolute_path,"File",file);
      }
    } 
    
    
  } 
  catch (Exception e) { 
    //println("Error while saving file: " + e);
  }
};
public class Permission{
  
  String permissionName;
  PApplet parent;

  public Permission(PApplet pParent,String permissionName) {
    parent = pParent;
    this.permissionName = permissionName;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
  };

  public void onPermissionResult(boolean granted) {
    if (!granted) {
      PApplet.println("User did not grant camera permission. "+ permissionName +" is disabled.");
    }
  };

};

I had the permissions enabled but I didn’t have the manifest file in my sketch folder! That’s why it didn’t work! Also a simpler solution is save.save(dataPath(“square.png”));

3 Likes