Get filepath and filename of selected gallery image in Android

后端 未结 9 2096
天命终不由人
天命终不由人 2020-11-27 04:47

I am creating an app which uploads a selected image from the gallery and uploads it to a web service. The webservice requires the filename of selected image plus a base64 e

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 05:33

    Hope this helps you

     if (Build.VERSION.SDK_INT <19){
    
        Intent intent = new Intent(); 
        intent.setType("image/jpeg");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, 
        getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
    
     }else{
    
          Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
          intent.addCategory(Intent.CATEGORY_OPENABLE);
          intent.setType("image/jpeg");
          startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
     }
    
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data)
     {
    
          super.onActivityResult(requestCode, resultCode, data);
          if (resultCode != Activity.RESULT_OK) return;
          if (null == data) return;
          Uri originalUri = null;
    
          if (requestCode == GALLERY_INTENT_CALLED) {
               originalUri = data.getData();
          } 
          else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
               originalUri = data.getData();
               final int takeFlags = data.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    
                // Check for the freshest data.
                getContentResolver().takePersistableUriPermission(originalUri,    takeFlags);
          }
          loadSomeStreamAsynkTask(originalUri);
    }
    

提交回复
热议问题