How to select an image from Android device

edited September 2016 in Android Mode

Hi! I need some help, please! I'm making an Android app where I want the user to select an image from the gallery to load into the app, I've seen the method selectInput() but it's only working in Java mode for me, not in Android. When trying it in Android mode it gives me this error: The method selectInput(String, String) is undefined for the type "nameOfTheSketch" And says that the Compile Failed.

Answers

  • edited April 2018 Answer ✓

    This is an example how to get an image from the gallery. Just tap the screen and follow the sequence. Something you need to keep in mind is that there are some code modifications based on what API you use. This code was tested using API 23. From what I saw online, managing access to the intent result (specifically to the file path) is divided in three possible ways: For older than API 11, API 11 to API 18, and newer than API 18.

    Kf

    import android.app.Activity;
    import android.content.Context;
    import android.widget.FrameLayout;
    import android.view.ViewParent;
    import android.widget.RelativeLayout;
    import android.text.Editable;
    import android.graphics.Color;
    import android.widget.Toast;
    import android.os.Looper;
    import android.view.WindowManager;
    import android.view.inputmethod.InputMethodManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    import android.widget.ImageView;
    import android.content.Intent;
    import android.provider.MediaStore;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.database.Cursor;
    import android.graphics.BitmapFactory;
    
    //REFERENCE: https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing#latest
    
    Activity act;
    Context mC;
    FrameLayout fl;
    
    
    ImageView imgView;
    
    @Override 
      public void onStart() {
      super.onStart();
    
      act = this.getActivity();
      mC= act.getApplicationContext();
    
    
    
      // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO
    
      // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO
    
    
      imgView = new ImageView(act);
      imgView.setLayoutParams(new RelativeLayout.LayoutParams(1000,1000));
      imgView.setX(100);
      imgView.setY(100);
    
    
      // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO
    
      //                 @@@@@@@@@@@@@@@@@@@@@@@@@@@
    
      // OOOOOOOOOOOOOOOO---------------------------OOOOOOOOOOOOOOOO
    
    
      fl = (FrameLayout)act.findViewById(0x1000);
      fl.addView(imgView);
    }
    
    
    void settings() {
      fullScreen();
    }
    
    void setup() {   
      orientation(LANDSCAPE);
      background(255, 0, 0);
      Looper.prepare();
    }
    
    void draw() {
      ;
    }
    
    
    
    void mouseReleased()
    {  
      pickImage();
    }
    
    
    //REFERENCE: http://stackoverflow.com/questions/5309190/android-pick-images-from-gallery
    //REFERENCE: http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/
    public void pickImage() {
      Intent intent = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      //MediaStore.Images.Media.INTERNAL_CONTENT_URI);
      intent.setType("image/*");
      startActivityForResult(intent, 1);
    }
    
    @ Override  // Here it should be "@ Override" with no space in btw
      void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode==1) {
        if (resultCode==Activity.RESULT_OK) {
          if (data!=null) {
    
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
            // Get the cursor
            Cursor cursor = mC.getContentResolver().query(selectedImage, 
              filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
    
            imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
          }
        } else {
          Toast.makeText(mC, "Cancelled", 
            Toast.LENGTH_SHORT).show();
        }
      } else if (resultCode == Activity.RESULT_CANCELED) {
        Toast.makeText(mC, "Cancelled", 
          Toast.LENGTH_SHORT).show();
      }
    }    
    
    public void onResume() {
    
      super.onResume();
    }
    

    ------Edited****

    Keyword: kf_keyword mediastore Android gallery

  • edited September 2016

    Thx Kf! I'll look at each line in detail, there is so much I need to learn! Thx again, have a good one!

  • Please notice the small edit in the above code. Code formatting in this forum doesn't like the @ symbol.

    Kf

  • @kfrajer, add a space between @ and Override. *-:)

  • edited December 2016

    Hi! I am trying to access gallery in my phone as well, so I tried the above code provided by @kfrajer . But I get two problems.(probably because I don't know about android at all...)

    Processing doesn't recognize this format...so can I just replace the below line:

    a href="/two/profile/Override">@Override</a

    with just

    @Override ??

    and also,

    void onActivityResult(int requestCode, int resultCode, Intent data)

    This line gives error saying : Cannot reduce the visibility of the inherited method from Fragment

    Does this have to do with the difference in API as mentioned or is it another problem?

    Any help would be appreciated!!!!! Thanks

  • @ Override without space in btw as described in line 99.

    I will get back to you later related to the onActivityResult.

    Kf

  • edited December 2016

    @kfrajer I put public in front of void and the error is gone... but now running the sketch fails, as I get this.

    I am working with API 18 and Processing 3.1.

    I have no clue, but I often get this same error when starting the sketch with other libraries and functions... Do you know what causes this error???

    FATAL EXCEPTION: Animation Thread java.lang.NullPointerException at processing.core.PGraphicsAndroid2D.backgroundImpl(Unknown Source) at processing.core.PGraphics.backgroundFromCalc(Unknown Source) at processing.core.PGraphics.background(Unknown Source) at processing.core.PGraphics.defaultSettings(Unknown Source) at processing.core.PGraphics.checkSettings(Unknown Source) at processing.core.PGraphicsAndroid2D.beginDraw(Unknown Source) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:856) FATAL EXCEPTION: Animation Thread java.lang.NullPointerException at processing.core.PGraphicsAndroid2D.backgroundImpl(Unknown Source) at processing.core.PGraphics.backgroundFromCalc(Unknown Source) at processing.core.PGraphics.background(Unknown Source) at processing.core.PGraphics.defaultSettings(Unknown Source) at processing.core.PGraphics.checkSettings(Unknown Source) at processing.core.PGraphicsAndroid2D.beginDraw(Unknown Source) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:856)

  • Cannot reduce the visibility of the inherited method from Fragment

    Prepend the method declaration w/ public. :-B

  • I think

    orientation(LANDSCAPE);

    is causing a problem to a certain degree as well!!! D:

  • The method startActivityForResult(Intent, int) is undefined for the type sketch_180321a

    i get this error when i use this code

  • @Pixelkar Target API, OS, Processing version and Android Mode version please.

    Did you setup any permissions? Which ones? I think you need WRITE_EXTERNAL_STORAGE and probably camera.

    Kf

  • @kfrajer My Target API is 24 , Moto G5 Plus Android Nougat ,Processing version 3.3.7,Android Mode Version 4.0.1

  • That's my complete error

    1. ERROR in C:\Users\NITANT\AppData\Local\Temp\android5932475971013812126sketch\app\src\main\java\processing\test\sketch_180321a\sketch_180321a.java (at line 138) startActivityForResult(intent, 1); ^^^^^^^^^^^^^^^^^^^^^^

    The method startActivityForResult(Intent, int) is undefined for the type sketch_180321a

    1 problem (1 error) FAILED

    FAILURE: Build failed with an exception.

    • Where: Build file 'C:\Users\NITANT\AppData\Local\Temp\android5932475971013812126sketch\app\build.gradle' line: 67

    • What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'.

      Process 'command 'D:\Processing\processing-3.3.7\java\bin\java.exe'' finished with non-zero exit value -1

    • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

    • Get more help at https://help.gradle.org

  • Basically I am very new to this android mode i tried SelectInput from Processing reference and also tried Select Library for android mode Non of them seem to work .

    i want to load images from device in my sketch and use the Pixel RGB Values using get(x,y); and perform certain process on image in Android device.

    i need Help in selecting Image file from Android Device Gallery Cheers

Sign In or Register to comment.