Search code examples
androidandroid-emulatorandroid-gallery

Get image content failing to open Gallery folders


Im creating an image converter app and need the user to select the image file from the device, im using an emulator and when i start the intent to pick a file from the gallery it displays the images in their folders but on clicking a folder, it just reopens the gallery and not the folder here's my code

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, getString(R.string.selectf)),
                FILE_SELECT_CODE);
    } catch (ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.", 
                Toast.LENGTH_SHORT).show();
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && resultCode == Activity.RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            //Log.d(TAG, "File Uri: " + uri.toString());
            // Get the path
            try {
                Path = getPath(this, uri);
                if(   Path.endsWith("png")  ||  Path.endsWith("gif")   ||    Path.endsWith("jpg")    ||    Path.endsWith("bmp")){
                    startprogress2();
                    progressDialog.onStart();
                    asyncTask = new loadAction();
                    asyncTask.execute();
                }
                else{
                    Toast.makeText(this, R.string.selecti,
                            Toast.LENGTH_SHORT).show();
                }
            }catch (URISyntaxException e) {
                e.printStackTrace();
                failtoloadtoast();
            }catch(Exception e){
                e.printStackTrace();
                failtoloadtoast();
            }
        }else{
            failtoloadtoast();
        }


    super.onActivityResult(requestCode, resultCode, data);
}

keep in mind i have to enable the user to be able to select image files also from a file manager not only gallery as some formats aren't read by gallery.


Solution

  • Use Like this:

    Intent:

     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                 startActivityForResult(intent, 0);
    

    To fetch that result:

       protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
           super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
           switch(requestCode) {
           case 0:
               if(resultCode == RESULT_OK){
                   Uri selectedImage = imageReturnedIntent.getData();
                   String[] filePathColumn = {MediaStore.Images.Media.DATA};
                   Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                   cursor.moveToFirst();
                   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  //file path of selected image
                   filePath = cursor.getString(columnIndex);
                   File f = new File(filePath);
                   filename= f.getName();
    
                   Toast.makeText(SiteViewFieldCreate.this, "Your Path:"+filePath, 2000).show();
                   Toast.makeText(SiteViewFieldCreate.this, "Your Filename:"+filename, 2000).show();
                   cursor.close();
    
               }
               break;
         }
         }
    

    Reference. Hope this will give you some solution.