save images in specific folders(Android)

edited May 2016 in Android Mode

Good evening,

I made a program for downloading images, the problem is I can not save them in an orderly way. when I have an image is no problem with the order, but my idea is to deal with over 300 different images and I want a quick program.

I want to create folders with different categories.

However although I get to do what I want in java, I fail in android.

What I can do?

this code work:

PImage a;
void setup(){ a = loadImage("http://img110.xooimage.com/files/4/5/b/mini-robot-4a0c8b8.png");
if(a != null){a.loadPixels();
a.save("aadfg.png");
}}
void draw(){if(a != null){a.loadPixels();image(a,20,20);}}

but, this code don´t work:

PImage a;
void setup(){ a = loadImage("http://img110.xooimage.com/files/4/5/b/mini-robot-4a0c8b8.png");
if(a != null){a.loadPixels();
a.save("folder/aadfg.png");
}}
void draw(){if(a != null){a.loadPixels();image(a,20,20);}}

this is the difference:

a.save("aadfg.png"); 
**||** 
a.save("folder/aadfg.png");

regards and thanks in advance.

Answers

  • @anthony20::

    you cannot do what you want in android mode with processing; each android app has a very precise structure with prefab folders which have always the same names; you can see that in eclipse. So if you want to save the files in # folders,the files and folders have to be created by coding and put on the internal or external storage sdcard, according to your needs & security permissions from the device and its system

  • @akenaton
    thanks for answer. How I can create a new folder on the internal storage of the app, I access the folder to save an image and finally to access that folder to load that image at any time? Do you can show me a sample code?

  • @anthony20:: -some example code; could be more sophisticated looking to the existing (or not folders...) -dont forget permissions write/read

            import android.os.Environment;
    
            void saveImage(){
                   try
                    {
                      String filename = "monimageProcessing.jpg";
                        String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myFolder");
                        save(directory + "/" + filename);
                        println("File saved successfully.");
                    } 
    
                    catch (Exception e) 
                    {
                        println("Error while saving file: " + e.getMessage());      
                    }
    
    
                }
    
                void mousePressed(){
                  saveImage();
                }
    
  • @akenaton Thanks for help me:

    I can not do work your code. Errors persist.

    But I found another solution that apparently works, although it will be quite laborious to apply this solution to my app.

    void setup()
    {
      String libraryPath = getApplicationInfo().dataDir;
     File folder = new File(libraryPath+"/folder");
    
      text(libraryPath, 55, 300);
    
         boolean success = true;
        if (!folder.exists()) {
          success = folder.mkdir();
        }
    PImage  a = loadImage("http://img110.xooimage.com/files/4/5/b/mini-robot-4a0c8b8.png");
    if(a != null){a.loadPixels();
    try{a.save(libraryPath + "/folder/a.png");}catch(Exception l){text("Mistake_1",22,22);}  
    }
    }
    
  • @anthony20:: the twos codes are exactly the same except that a) you are saving inside the app package, like a library, and not in the sdcard; that is your choice (you cannot see or change the files with ddms...) b) you create a folder ("folder") and i assumed that the folder exists already, see my post "could be more sophisticated looking to the existing (or not folders...)"

  • @akenaton

    I am try use your code:

    import android.os.Environment;
     void setup(){background(222,111,55);fill(11,222,156);saveImage();}
    void saveImage(){
           try
            {
              String filename = "monimageProcessing.jpg";
                String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myFolder");
             PImage A =   loadImage("http://img110.xooimage.com/files/4/5/b/mini-robot-4a0c8b8.png");
                A.save(directory + "/" + filename);
                text("File saved successfully.",55,55);
            } 
    
            catch (Exception e) 
            {
                text("Error while saving file: ",55,55);      
            }
    
    
        }
    
        void mousePressed(){
          saveImage();
        }
    

    Manifiest: <?xml version="1.0"?>

    -<manifest package="" android:versionName="1.0" android:versionCode="1" xmlns:android="http://schemas.android.com/apk/res/android">
    
    <uses-sdk android:minSdkVersion="10"/>
    
    
    -<application android:label="" android:icon="@drawable/icon" android:debuggable="true">
    
    
    -<activity android:name="">
    
    
    -<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>
    

    I need some additional permission?

  • the error is probably that the folder "myFolder" does not exist; in that case you have to create it like in your code; PS: not sure at all that it is a good idea to download while saving! - you have probably to make a runnable. Try that (permissions + some image in your data folder with the same name)

            import android.os.Environment;
    
             String filename = "monimageProcessing.jpg";
             PImage img;
    
             void setup(){
               background(222,111,55);
             fill(11,222,156);
             img = loadImage(filename);
             saveImage(filename);
    
             };
    
    
            void saveImage(String s){
                   try
                    {
    
                        String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myFolder");
                         File myFolder = new File(directory);
    
                         boolean success = true;
                        if (!myFolder.exists()) {
                  success = myFolder.mkdirs();
                        }
    
                        img.save(directory + "/" + s);
                        println("File saved successfully.");
                    } 
    
                    catch (Exception e) 
                    {
    
                        println("Error while saving file: " + e);      
                    }
    
    
                }
    
  • edited March 2015

    Hi @akenaton Thanks so much for help me:

    The mistake was that this had not permissions of Intenet, the picture is not loaded and was an exception to be null and try to save it.

    I now have two solutions, I will try to use the internal storage, as this does not require additional permissions.

    import android.os.Environment;
     void setup(){background(222,111,55);fill(11,222,156);saveImage();}
    void saveImage(){
         String directory = getApplicationInfo().dataDir + "/myFolder";
    
    PImage A = null;
           try
            {
              String filename = "monimageProcessing.jpg";
      try
            {             File folder = new File(directory);     boolean success = true;
        if (!folder.exists()) {
          success = folder.mkdir();
        }}
    catch(Exception a){text("JJJJJ",77,500);}             
     try{         A =   loadImage("http://img110.xooimage.com/files/4/5/b/mini-robot-4a0c8b8.png");
             image(A,200,200);
                 }
    catch(Exception a){text("KKKKK",77,700);}     
            try{    A.save(directory + "/" + filename);
                text("File saved successfully.",55,355);}catch(Exception e){ text("File NOT saved successfully.",55,255);}
            } 
    
            catch (Exception e) 
            {            //String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myFolder");
    
                text("Error while saving file: ",55,55);       text(directory,55,155);      
            }
    
    
        }
    
        void mousePressed(){
          saveImage();
        }
    
  • Answer ✓

    @ anthony20 ::: as for me sdcard is better because you can SEE what happens at any time, even after having exported the app...

  • thanks for the advice and help @akenaton

    I've successfully adapted my code of download images to android. I created several features that allow automatically create folders and it seems that saved the 200 images is neat, quick and correct.

    Now it is the hardest part, which is to make everything work properly within the main app.

  • Answer ✓

    does someone know how to safe a picture taken from a camera

  • Answer ✓

    @mathijsvann=== supposing that you have taken the photo (eg: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE)) you have only to add an extra to the intent (intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri) and fix the fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); after that you can use the same code i have given to anthony20 for saving either on the standard folders created by android or elsewhere...

Sign In or Register to comment.