Soru & Cevap

Mevcut Layoutun Screenshotunu Alma ve Bunu Paylaşma ...

13.05.2017 - 12:54

Görsel kaldırıldı.Görsel kaldırıldı.



Ekran görüntüsünü alıp bunu facebook,whatsapp benzeri yerlerde paylaşmak istiyorum fakat resim çıkmıyor neyi eksik yapıyorum ?Facebook sdksini yukledim gerekli izinleri verdim ama sonuç bu..

Activity:

findViewById(R.id.buttonaskmetrepaylas).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);

            }

        });
    }

    private Bitmap takeScreenshot() {

        // create bitmap screen capture
        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.metcap);
        linearLayout.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(linearLayout.getDrawingCache());
        return bitmap;
    }


    public void saveBitmap(Bitmap bitmap) {

        String imagePa = Environment.getExternalStorageDirectory().toString() + "/screenshot.jpg";
        File imagePath = new File(imagePa);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(new File(imagePa));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }

        openScreenshot(imagePath);
    }

    private void openScreenshot(File imagePa) {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imagePa));
        intent.setType("image/jpeg");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent.createChooser(intent, "Share via"));

    }

 

 

AndroidManifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


 

170 Görüntülenme

5 Cevap

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

Profile picture for user turkordu
turkordu
15.05.2017 - 03:06

İşte çözüm:

Activity sınıfının içinde oncreatein dışına şu kodları ekleyin:

private void askForPermission(String permission, Integer requestCode) {
        if (ContextCompat.checkSelfPermission(Activity.this, permission) != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(Activity.this, permission)) {

                //This is called if user has denied the permission before
                //In this case I am just asking the permission again
                ActivityCompat.requestPermissions(Activity.this, new String[]{permission}, requestCode);

            } else {

                ActivityCompat.requestPermissions(ResultActivity.this, new String[]{permission}, requestCode);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {

        }
    }

 

Daha sonra Oncreate içerisinde oluşturduğunuz butona şu kodu ekleyin :

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               
              askForPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,WRITE_EXST);

            }

        });


 

Profile picture for user turkordu
turkordu
14.05.2017 - 09:20
05-14 14:18:21.146 2733-2733/com.example.turko.burcrehberim W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Sun May 14 14:18:21 EDT 2017.jpg: open failed: EACCES (Permission denied)
05-14 14:18:21.146 2733-2733/com.example.turko.burcrehberim W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
05-14 14:18:21.146 2733-2733/com.example.turko.burcrehberim W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at com.example.turko.burcrehberim.LoveMeterResultActivity.takeScreenshot(LoveMeterResultActivity.java:288)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at com.example.turko.burcrehberim.LoveMeterResultActivity.access$200(LoveMeterResultActivity.java:60)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at com.example.turko.burcrehberim.LoveMeterResultActivity$3.onClick(LoveMeterResultActivity.java:265)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.view.View.performClick(View.java:5198)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.os.Looper.loop(Looper.java:148)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at libcore.io.Posix.open(Native Method)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
05-14 14:18:21.147 2733-2733/com.example.turko.burcrehberim W/System.err: 	... 14 more

Bu manifestle alakalı bi durum fakat çözemedim

Batuhan Orhan
14.05.2017 - 10:30
manifeste ilgili izinleri eklediniz mi ?
Serhat
14.05.2017 - 10:33
Ekledim
Serhat
14.05.2017 - 10:35
http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android Hatayı araştırdım böyle bişey buldum ama nereye eklemem gerektiğini bulamadım
Batuhan Orhan
14.05.2017 - 10:38
http://prntscr.com/f7subb android manifestin içine şekildeki gibi izin eklenecek.
Serhat
14.05.2017 - 10:51
O kısımda sorun yok aplication tagının dışında kod
Serhat
15.05.2017 - 01:17
5.0 da çalışıyor sorun 6.0 ve yukarısı
Batuhan Orhan
15.05.2017 - 01:25
6.0 ve üstünde uygulama izin yöntemleri değiştiği için olmayacaktır. 6.0 için gerekli olan yazma izinlerini uygulamana eklemelisin.
Serhat
15.05.2017 - 02:20
hallettim yardımların için teşekkürler çözümü burada paylaşıcam
Profile picture for user turkordu
turkordu
14.05.2017 - 02:24
findViewById(R.id.buttonaskmetrepaylas).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takeScreenshot();
            }
        });
        
    }

    public void takeScreenshot() {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            openScreenshot(imageFile);
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
    }

    public void openScreenshot(File imageFile) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(imageFile);
        intent.setDataAndType(uri, "image/*");
        startActivity(intent);
    }

Fileoutputstream ve takescreenshot satırında hata alıyorum

Batuhan Orhan
14.05.2017 - 08:22
hatayı paylaşır mısınız ?
Profile picture for user turkordu
turkordu
13.05.2017 - 09:22

bunu denedim fakat butona bağlayamadım

Serhat
14.05.2017 - 02:22
click metodu içerisine takescreenshot() fonksiyonunu ekliyiyorum hiçbir sonuç vermiyor.
Batuhan Orhan
13.05.2017 - 01:18
Butonun Click metodunda attığım linkteki takescreenshot fonksiyonunu çağırırsanız olucaktır.
Profile picture for user bthnorhan
bthnorhan
13.05.2017 - 02:43

Merhabalar öncelikle.

http://stackoverflow.com/a/5651242 cevabındaki kodu deneyip sonucu paylaşır mısınız ? 

Teşekküler kolay gelsin.