Soru & Cevap

Webview de dosya ekleme ...

18.11.2019 - 08:04

Merhaba arkadaşlar android studio dan çok fazla anlamam izlediğim videolar kadarıyla bi webview uygulaması yaptım çeşitli yerlerde dosya izinleri hakkında kodlar buldum ama çalıştıramadım Sorun şu; Akvaryumasiatani.com sitesinde yeni bir konu veya mevcut konuya cevap yazarken resim ekle butonu var webview de de bu buton görülüyo ancak resim ekle butonuna tıklandığı zaman herhangi bişey olmuyor bu konuda sizlerden yardım istiyorum

124 Görüntülenme

3 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user barisdemirok06
barisdemirok06
19.11.2019 - 01:19

Merhabalar şöyle bir kod buldum ve kendime göre uyarladım. herşeyi eksiksik yaptım gibi görünüyor

Şu anda şöyle bir sıkıntı var uygulama içinden dosya ekle butonuna tıklandığı zaman resimler açılıyor, resim seçiliyor ama seçilen resim yüklenmiyor

kodlarım aşağıdaki gibidir hata nerde acaba ? uygulamadan bakmak içinde uygulama linki :

https://play.google.com/store/apps/details?id=com.akvaryum.akvaryumasistan

 

kodlar : 

package com.akvaryum.akvaryumasistan;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.content.Context;
import android.app.ProgressDialog;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.content.DialogInterface;
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.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    private final static int FCR = 1;
    WebView webView;
    private String mCM;
    private ValueCallback<Uri> mUM;
    private ValueCallback<Uri[]> mUMA;
    Context context = this;
    CustomWebViewClient webViewClient;
    ProgressDialog mPregressDialog;
    NetWorkChangeReceiver receiver;

    @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Reciveri register ediyoruz
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new NetWorkChangeReceiver();
        registerReceiver(receiver, filter);
        //SwipeRefleshLayout
        final SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeContanier);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!internetBaglantisiVarMi()) {
                    Intent i = new Intent(context, AyarlarActivity.class);
                    startActivity(i);
                    finish();
                }
                webView.reload();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        //mPregressDialog
        mPregressDialog = new ProgressDialog(context);
        mPregressDialog.setMessage("Yükleniyor...");

        webView = findViewById(R.id.webView);
        if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        }
        assert webView != null;

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(0);
            webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else if (Build.VERSION.SDK_INT >= 19) {
            webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else if (Build.VERSION.SDK_INT < 19) {
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webView.setWebViewClient(new Callback());
        //webView.loadUrl("https://akvaryumasistani.com/");
        webView.loadUrl("https://akvaryumasistani.com/");

        webView.setWebChromeClient(new WebChromeClient() {

            //For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {

                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
            }

            // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
            public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                MainActivity.this.startActivityForResult(
                        Intent.createChooser(i, "File Browser"),
                        FCR);
            }

            //For Android 4.1+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
            }

            //For Android 5.0+
            public boolean onShowFileChooser(
                    WebView webView, ValueCallback<Uri[]> filePathCallback,
                    WebChromeClient.FileChooserParams fileChooserParams) {

                if (mUMA != null) {
                    mUMA.onReceiveValue(null);
                }

                mUMA = filePathCallback;
                // Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


                Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                contentSelectionIntent.setType("*/*");
//                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_INTENT, contentSelectionIntent);
                // chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
                // chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
                startActivityForResult(chooserIntent, FCR);

                return true;
            }
        });
    }

    public class Callback extends WebViewClient {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else{
            AlertDialog.Builder alertdialog=new AlertDialog.Builder(context);
            alertdialog.setMessage("Uygulamayı Kapatmak İstediğinden Emin Misin?");
            alertdialog.setCancelable(false);
            alertdialog.setPositiveButton("Evet", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);

                }
            });
            alertdialog.setNegativeButton("Hayır", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            AlertDialog.Builder builder = alertdialog;
            AlertDialog alert=alertdialog.create();
            alert.show();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);

    }

    boolean internetBaglantisiVarMi(){
        ConnectivityManager conMngr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMngr.getActiveNetworkInfo() != null && conMngr.getActiveNetworkInfo() .isAvailable() && conMngr.getActiveNetworkInfo() .isConnected()) {
            return true;
        }else{
            return false;
        }
    }
}

 

Profile picture for user barisdemirok06
barisdemirok06
18.11.2019 - 11:54

O izinler ve bulduğum bütün diğer izinler ekli hocam

Google play izinlere bakıldığı zaman dosya ve klasörlere erişim gösteriyor ama gel gelelim uygulama içinde iken dosya seç e tıklandığı zaman herhangi bir eylem olmuyor 

/sites/default/files/cevaplar/dosya/2020/file-adsiz_135.png /sites/default/files/cevaplar/dosya/2020/file-1111111111111111.png
Profile picture for user ensaryusufyilmaz
ensaryusufyilmaz
18.11.2019 - 08:18

Merhaba Barış

Yapman gereken aslında çok basit. Android Manifest dosyana 2 adet izin eklemen sanırım yeterli olacaktır. Bu izinler sayesinde uygulama depolamaya ve kameraya erişim sağlayacaktır. Tek bir dez avantajın olacak oda kamera iznini Android manifeste eklediğin zaman kamerası olamayan cihazlar uygulamanı kullanamaycaklar. Bunu oluşturduğun çıktını (apk,app bundle) Google Play den güncellerken ya da yükleme yaparken göreceksin. Akıllı saatler ve bazı tabletler uygulamanı kullanamayacak. 

Gerekli izinler:

<uses-permission android:name=”android.permission.CAMERA” />  (Kamera İzni)
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />  (Cihazdaki Dosyalara Erişim İzini)

Bu şekilde bir deneme yapıp lütfen beni bilgilendir.

Başarılar...

Ensar Yusuf Yılmaz

Founder / seskit - Türkçe Sesli Kitaplar