Arama butonu
Bu konudaki kullanıcılar: 1 misafir
0
Cevap
50
Tıklama
0
Öne Çıkarma
Android WebView de Dosyalardan ve kamera ile goruntu yukleyebılme
H
2 ay
Er
Konu Sahibi

Android kullanmayı bilmiyorum.. Web sayfamı basit bir webview ile goruntulemek ıstıyorum .WEb sayfamda dosya yukleme dugmem var ve bu dugmeye bastıgımda kamera veya dosya yukleme secenegının acılmasını secımden sonra dosya yukleme ıslemının basarı ıle gerceklesmesını ıstıyorum.. Paylasacagım kodda dosya yukleme ıslemı gerceklesıyor fakat kamera ıle fgoruntu aktaramıyorum... bu konuda yardımcı olup kodu udzeletebılırsenız sevınırım.. Saygılar..

package www.aaaaaa.aaaaaaapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
private WebView webView;
private RelativeLayout noInternetLayout;
private static final int FILE_CHOOSER_REQUEST_CODE = 1;
private ValueCallback<Uri[]> fileUploadCallback;
private String mCameraPhotoPath;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

noInternetLayout = findViewById(R.id.noInternetLayout);

webView = findViewById(R.id.wbView);
webView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (!isConnected()) {
webView.setVisibility(View.GONE);
noInternetLayout.setVisibility(View.VISIBLE);
}
}

@Override
public void onPageFinished(WebView view, String url) {
webView.setVisibility(View.VISIBLE);
}
});

webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
fileUploadCallback = filePathCallback;
Intent intent = fileChooserParams.createIntent();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}

if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
mCameraPhotoPath = photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
}

Intent chooserIntent = Intent.createChooser(intent, "Dosya Seç");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePictureIntent});

startActivityForResult(chooserIntent, FILE_CHOOSER_REQUEST_CODE);
return true;
}
});

webView.loadUrl("https://www.aaaaaa.com/");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_REQUEST_CODE) {
if (fileUploadCallback != null) {
if (resultCode == RESULT_OK) {
if (data != null && data.getData() != null) { // Handle gallery selection
Uri[] result = new Uri[]{data.getData()};
fileUploadCallback.onReceiveValue(result);
} else if (mCameraPhotoPath != null) { // Handle camera capture
Uri capturedImageUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", new File(mCameraPhotoPath));
fileUploadCallback.onReceiveValue(new Uri[]{capturedImageUri});
} else {
// Handle no file selected or camera capture error
Log.e("onActivityResult", "No file selected or camera capture error");
fileUploadCallback.onReceiveValue(null);
}
} else {
// Debug: Hata olabilir, resultCode RESULT_OK değil
Log.e("onActivityResult", "Result code is not RESULT_OK");
fileUploadCallback.onReceiveValue(null);
}
fileUploadCallback = null;
}
}
}

private boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(null);
return File.createTempFile(imageFileName, ".jpg", storageDir);
}
}



DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.