Question: I'm building an Android app that needs to be able to go thru file explorer and pick a file. as of now, have a button
I'm building an Android app that needs to be able to go thru file explorer and pick a file. as of now, have a button (that doesn't do anything yet) from app > res > menu > main.xml but I need pressing it to take my to the file explorer, from which a user can chose a file that ultimately will be uploaded.
How and where should I code this, both in xml and java? I've looked online, but most Google search results with "file explorer" and "android studio" in the query return posts about people using file explorer on their emulator more than integrating it's use into the app.
Here's my code for my MainActivity:
package com.example.andrewt.UROC_Spring2017; (name).(file) import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.widget.Toast; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public void showFileChooser(View view) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); //all files //intent.setType("text/xml"); //XML file only intent.addCategory(Intent.CATEGORY_OPENABLE); //startActivity(intent); try { startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 1); } catch (android.content.ActivityNotFoundException ex) { // Potentially direct the user to the Market with a Dialog Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); } } }
and here's my ActivityMain:
xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.andrewt.UROC_Spring2017.MainActivity"> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:hint="@string/edit_message" android:inputType="textPersonName" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/button" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginStart="16dp" android:text="@string/button_send" app:layout_constraintBaseline_toBaselineOf="@+id/editText" app:layout_constraintLeft_toRightOf="@+id/editText" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="16dp" android:layout_marginTop="16dp" android:onClick="showFileChooser [MainActivity]" android:text="@string/button_find" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintRight_toRightOf="parent" /> android.support.constraint.ConstraintLayout>
When I try to run it, my logcat shows this:
4-05 19:25:08.691 2721-2721/? I/art: Not late-enabling -Xcheck:jni (already on) 04-05 19:25:08.692 2721-2721/? W/art: Unexpected CPU variant for X86 using defaults: x86 04-05 19:25:08.815 2721-2721/? W/System: ClassLoader referenced unknown path: /data/app/com.example.andrewt.UROC_Spring2017-1/lib/x86 04-05 19:25:08.838 2721-2721/? I/InstantRun: starting instant run server: is main process 04-05 19:25:08.932 2721-2721/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 04-05 19:25:09.236 2721-2770/? I/OpenGLRenderer: Initialized EGL, version 1.4 04-05 19:25:09.236 2721-2770/? D/OpenGLRenderer: Swap behavior 1 04-05 19:25:09.237 2721-2770/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 04-05 19:25:09.237 2721-2770/? D/OpenGLRenderer: Swap behavior 0 04-05 19:25:09.304 2721-2721/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 04-05 19:25:10.466 2721-2721/com.example.andrewt.UROC_Spring2017 D/AndroidRuntime: Shutting down VM --------- beginning of crash 04-05 19:25:10.466 2721-2721/com.example.andrewt.UROC_Spring2017 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.andrewt.UROC_Spring2017, PID: 2721 java.lang.IllegalStateException: Could not find method showFileChooser [MainActivity](View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
