Android file selector?

I've been trying all the solutions I can find on the forums, but I can't get any of them to work. I just need a simple file selector, something that will return a file path that I can load as an image or sound clip into Processing.

Thanks for any help.

Answers

  • yeah I tried that code, I get the file browser popping up - I then have to a select a picture twice before it progresses, but then nothing happens - the onActivityResult method never gets called.

  • Hello, please try the following code. Bascially, the following code is from the link kf pointed. The code, I have tested, it worked on my Android phone, Android Version 6.0. when you run the code, select imge file, it will print the path on your android screen, you can do stuff to the path.

                import android.app.Activity;
                import android.content.Context;
                import android.widget.FrameLayout;
                import android.widget.RelativeLayout;
                import android.widget.Toast;
                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: <a href="https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing#latest" target="_blank" rel="nofollow">https://forum.processing.org/two/discussion/16539/how-to-manually-create-android-textfield-in-processing#latest</a>;
    
                Activity act;
                Context mC;
                FrameLayout fl;
                ImageView imgView;
                String imgDecodableString ="";
                PImage img = loadImage(null);
                @Override
                  public void onStart() {
                  super.onStart();
                  act = this.getActivity();
                  mC= act.getApplicationContext();
                  imgView = new ImageView(act);
                  imgView.setLayoutParams(new RelativeLayout.LayoutParams(1000, 1000));
                  fl = (FrameLayout)act.findViewById(0x1000);
                  fl.addView(imgView);
                }
    
                void setup() {   
                  size(displayWidth, displayHeight, P3D);
                  textAlign(CENTER, CENTER);
                  imageMode(CENTER);
                  textSize(32);
                }
    
                void draw() {
                  background(150, 0, 0);
                  //image(img, width/2, height/2);
                  pushMatrix();
                  translate(width/2, height/2);
                  rotateZ(degrees(90));
                  text(imgDecodableString, 0, 0);
                  popMatrix();
                }
    
                void mouseReleased()
                {  
                  pickImage();
                }
    
    
    
                //public void onResume() {
                //  super.onResume();
                //}
    
                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 
                  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]);
                        imgDecodableString = cursor.getString(columnIndex);// my edit
                        cursor.close();
                        //imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
                        //img = loadImage(imgDecodableString);//my eidt
    
                      }
                    } else {
                      Toast.makeText(mC, "Cancelled", 
                        Toast.LENGTH_SHORT).show();
                    }
                  } else if (resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(mC, "Cancelled", 
                      Toast.LENGTH_SHORT).show();
                  }
                }
    
  • thanks for this - will try it later and get back to you :)

  • @Bizkit_j

    The forum formatting changes all your @ Override references. To fix it, write "@ Override" instead of @.Override aka. add a space between @ and Override. This will solve the formatting issue in your post.

    Kf

Sign In or Register to comment.