2017-10-23 6 views
0

Hey und danke für deine Zeit. In meiner Seite, die in Teh webview meiner App geladen wird, ist ein Foto-Upload:Android Webview Bild von der Galerie oder Kamera hochladen

<form action="?pb=1" method="post" enctype="multipart/form-data"> 
<input id="pbbutton" type="file" title="Profielbild ändern" name="datei" onchange="this.form.submit()"> 

<label id="pblabel" for="pbbutton"><img id="profilbild" title="Profilbild ändern" class="userpb" src="./users/<?php echo htmlspecialchars($userid); ?>/pb" alt="Bild nicht gefunden" onerror="this.src='./img/no_pb.png';"></label> 

</form> 

Es ein Foto uploades, wenn Sie in der Regel pic einer Ihrer Galerie etc ...

Wie kann ich machen, Wenn ich auf diese Eingabe klicke, kann ich ein Bild auswählen und dann entdeckt die Webseite die Änderung (js: onchange) Ich habe schon etwas versucht, aber es wird das Bild nicht hochladen, nachdem ich es ausgewählt habe. Hier meine Codierung für die imgupload:

public class MainActivity extends Activity { 



//For IMG Upload 
private static final int INPUT_FILE_REQUEST_CODE = 1; 
    private static final int FILECHOOSER_RESULTCODE = 1; 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private ValueCallback<Uri> mUploadMessage; 
    private Uri mCapturedImageURI = null; 
    private ValueCallback<Uri[]> mFilePathCallback; 
    private String mCameraPhotoPath; 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { 
       super.onActivityResult(requestCode, resultCode, data); 
       return; 
      } 
      Uri[] results = null; 
      // Check that the response is a good one 
      if (resultCode == Activity.RESULT_OK) { 
       if (data == null) { 
        // If there is not data, then we may have taken a photo 
        if (mCameraPhotoPath != null) { 
         results = new Uri[]{Uri.parse(mCameraPhotoPath)}; 
        } 
       } else { 
        String dataString = data.getDataString(); 
        if (dataString != null) { 
         results = new Uri[]{Uri.parse(dataString)}; 
        } 
       } 
      } 
      mFilePathCallback.onReceiveValue(results); 
      mFilePathCallback = null; 
     } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { 
      if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) { 
       super.onActivityResult(requestCode, resultCode, data); 
       return; 
      } 
      if (requestCode == FILECHOOSER_RESULTCODE) { 
       if (null == this.mUploadMessage) { 
        return; 
       } 
       Uri result = null; 
       try { 
        if (resultCode != RESULT_OK) { 
         result = null; 
        } else { 
         // retrieve from the private variable if the intent is null 
         result = data == null ? mCapturedImageURI : data.getData(); 
        } 
       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), "activity :" + e, 
          Toast.LENGTH_LONG).show(); 
       } 
       mUploadMessage.onReceiveValue(result); 
       mUploadMessage = null; 
      } 
     } 
     return; 
    } 

    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES); 
     File imageFile = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 
     return imageFile; 
    } 






    /** 
* WebChromeClient subclass handles UI-related calls 
* Note: think chrome as in decoration, not the Chrome browser 
*/ 
public class GeoWebChromeClient extends WebChromeClient { 
    @Override 
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { 
     // Always grant permission since the app itself requires location 
     // permission and the user has therefore already granted it 
     callback.invoke(origin, true, false); 
    } 

} 

    WebView mWebView; 


    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     if(checkAndRequestPermissions()) { 
      // carry on the normal flow, as the case of permissions granted. 
     } 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mWebView = (WebView) findViewById(R.id.webView); 
     mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     mWebView.setWebViewClient(new GeoWebViewClient()); 
     // Below required for geolocation 
     mWebView.getSettings().setAppCacheEnabled(true); 
     mWebView.getSettings().setDatabaseEnabled(true); 
     mWebView.getSettings().setDomStorageEnabled(true); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.getSettings().setAllowFileAccess(true); 

     mWebView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       mWebView.loadUrl("file:///android_asset/noinet.html"); 

      } 
     }); 
     mWebView.getSettings().setGeolocationEnabled(true); 
     mWebView.setWebChromeClient(new GeoWebChromeClient()); 
     mWebView.getSettings().setBuiltInZoomControls(false); 
     mWebView.loadUrl("https://www.youtivity.org"); 

     mWebView.setWebChromeClient(new WebChromeClient() { 

      //FOR IMG UPLOAD 
      // For Android 5.0 
      public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) { 
       // Double check that we don't have any existing callbacks 
       if (mFilePathCallback != null) { 
        mFilePathCallback.onReceiveValue(null); 
       } 
       mFilePathCallback = filePath; 
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
        // Create the File where the photo should go 
        File photoFile = null; 
        try { 
         photoFile = createImageFile(); 
         takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
        } catch (IOException ex) { 
         // Error occurred while creating the File 
         Log.e(TAG, "Unable to create Image File", ex); 
        } 
        // Continue only if the File was successfully created 
        if (photoFile != null) { 
         mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
           Uri.fromFile(photoFile)); 
        } else { 
         takePictureIntent = null; 
        } 
       } 
       Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
       contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
       contentSelectionIntent.setType("image/*"); 
       Intent[] intentArray; 
       if (takePictureIntent != null) { 
        intentArray = new Intent[]{takePictureIntent}; 
       } else { 
        intentArray = new Intent[0]; 
       } 
       Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
       chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true); 
       chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
       chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); 
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
       startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 
       return true; 
      } 

     }); 

    } 






    @Override 
    public void onBackPressed() { 
     // Pop the browser back stack or exit the activity 
     if (mWebView.canGoBack()) { 
      mWebView.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 
} 

Ich hoffe, Sie können einen großen Tag vielleicht

Antwort

Verwandte Themen