There are various situations in which you have to give user a facility to select an image from their android devices. For this giving user a File explorer could be a wrong thing, you have to open the gallery so that the user can select an image. Here is the code for this scenario.
Firstly you have to create a button on whose click we will open devices library. But before that you should have a READ_EXTERNAL_STORAGE permission.
1 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
Also please remember that from API 23 and above there are Run-time permissions so you have to check for them like I did.
So I am going to have a Button and a EditText. On Buttons click we will open our gallery or any Image Viewer and in the EditText we will set the name of the image we selected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Button bt = (Button) findViewById(R.id.choose_file_button); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(ProductActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(ProductActivity.this, new String[]{android.READ_EXTERNAL_STORAGE}, 1); } else { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 1); } } else { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 1); } } }); |
This line will create a dialog that will ask user for a permission, so we have to handle the user response to the permission as well and also the onActivityResult() method as well after all it will run when you select an image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, requestCode); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri selectedImageUri = data.getData(); String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(this, selectedImageUri, projection, null, null, null); Cursor cursor = loader.loadInBackground(); if(cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); Uri imageUri = Uri.parse(cursor.getString(column_index)); ((EditText) findViewById(R.id.selected_filename)).setText(imageUri.getLastPathSegment()); } cursor.close(); } } |
And you have the image you wanted. You can load this image by converting it in a Bitmap or anything you want to do with it. For uploading this file to server you can follow this link.