2015-09-09 2 views
0

Im, пытающийся сделать снимок экрана в прямом эфире моей камеры ip. Я могу сделать это и сохранить его на моей SD-карте, проблема заключается в том, что я получаю («К сожалению, ваши приложения остановились») после нажатия кнопки скриншота. Это мой код ...java.lang.IllegalStateException: Не удалось найти метод?

package com.example.isec.isec; 



    import java.io.ByteArrayOutputStream; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.util.Random; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.graphics.Bitmap; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.graphics.PorterDuff; 
    import android.graphics.PorterDuffXfermode; 
    import android.graphics.Rect; 
    import android.graphics.Typeface; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.util.AttributeSet; 
    import android.util.Log; 
    import android.view.SurfaceHolder; 
    import android.view.SurfaceView; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.Toast; 

public class MjpegView extends SurfaceView implements        SurfaceHolder.Callback { 
private static final String TAG = "MjpegView"; 

public final static int POSITION_UPPER_LEFT = 9; 
public final static int POSITION_UPPER_RIGHT = 3; 
public final static int POSITION_LOWER_LEFT = 12; 
public final static int POSITION_LOWER_RIGHT = 6; 
public final static int SIZE_STANDARD = 1; 
public final static int SIZE_BEST_FIT = 4; 
public final static int SIZE_FULLSCREEN = 8; 

//private MjpegViewfunc clickFunc; 
private MjpegViewThread thread; 
private MjpegInputStream mIn = null; 
private boolean showFps = false; 
private boolean mRun = false; 
private boolean surfaceDone = false; 
private Paint overlayPaint; 
private int overlayTextColor; 
private int overlayBackgroundColor; 
private int ovlPos; 
private int dispWidth; 
private int dispHeight; 
private int displayMode; 


    public void clickFunc(View view) { 

     if (view.getId() == R.id.button9) ; 
     // Toast.makeText(MjpegViewfunc.this, "Button Clicked", Toast.LENGTH_SHORT).show(); 

     try { 
      //Bitmap bitmap = takeScreenShot(); 
      MjpegView.this.thread.run(); 
      // Bitmap bitmap = takeScreenShot(activity, ResourceID); 
      // saveBitmap(bitmap); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return; 
    } 


    public class MjpegViewThread extends Thread { 
     private SurfaceHolder mSurfaceHolder; 
     private int frameCounter = 0; 
     private long start; 
     private Bitmap ovl; 

     public MjpegViewThread(SurfaceHolder surfaceHolder, Context context) { 
      mSurfaceHolder = surfaceHolder; 
     } 

     private Rect destRect(int bmw, int bmh) { 
      int tempx; 
      int tempy; 
      if (displayMode == MjpegView.SIZE_STANDARD) { 
       tempx = (dispWidth/2) - (bmw/2); 
       tempy = (dispHeight/2) - (bmh/2); 
       return new Rect(tempx, tempy, bmw + tempx, bmh + tempy); 
      } 
      if (displayMode == MjpegView.SIZE_BEST_FIT) { 
       float bmasp = (float) bmw/(float) bmh; 
       bmw = dispWidth; 
       bmh = (int) (dispWidth/bmasp); 
       if (bmh > dispHeight) { 
        bmh = dispHeight; 
        bmw = (int) (dispHeight * bmasp); 
       } 
       tempx = (dispWidth/2) - (bmw/2); 
       tempy = (dispHeight/2) - (bmh/2); 
       return new Rect(tempx, tempy, bmw + tempx, bmh + tempy); 
      } 
      if (displayMode == MjpegView.SIZE_FULLSCREEN) { 
       return new Rect(0, 0, dispWidth, dispHeight); 
      } 
      return null; 
     } 

     public void setSurfaceSize(int width, int height) { 
      synchronized (mSurfaceHolder) { 
       dispWidth = width; 
       dispHeight = height; 
      } 
     } 


     public Bitmap makeFpsOverlay(Paint p, String text) { 
      Rect b = new Rect(); 
      p.getTextBounds(text, 0, text.length(), b); 
      int bwidth = b.width() + 2; 
      int bheight = b.height() + 2; 
      Bitmap bm = Bitmap.createBitmap(bwidth, bheight, Bitmap.Config.ARGB_8888); 
      Canvas c = new Canvas(bm); 
      p.setColor(overlayBackgroundColor); 
      c.drawRect(0, 0, bwidth, bheight, p); 
      p.setColor(overlayTextColor); 
      c.drawText(text, -b.left + 1, (bheight/2) - ((p.ascent() + p.descent())/2) + 1, p); 
      return bm; 
     } 





     public void run() { 
      start = System.currentTimeMillis(); 
      PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OVER); 
      Bitmap bm; 
      int width; 
      int height; 
      Rect destRect; 
      Canvas c = null; 
      Paint p = new Paint(); 
      String fps; 
      while (mRun) { 
       if (surfaceDone) { 
        try { 
         c = mSurfaceHolder.lockCanvas(); 
         synchronized (mSurfaceHolder) { 
          try { 
           bm = mIn.readMjpegFrame(); 
           destRect = destRect(bm.getWidth(), bm.getHeight()); 
           c.drawColor(Color.BLACK); 
           c.drawBitmap(bm, null, destRect, p); 
           if (showFps) { 
            p.setXfermode(mode); 
            if (ovl != null) { 
             height = ((ovlPos & 1) == 1) ? destRect.top : destRect.bottom - ovl.getHeight(); 
             width = ((ovlPos & 8) == 8) ? destRect.left : destRect.right - ovl.getWidth(); 
             c.drawBitmap(ovl, width, height, null); 
            } 

            //return bm; 

            p.setXfermode(null); 
            frameCounter++; 
            if ((System.currentTimeMillis() - start) >= 1000) { 
             fps = String.valueOf(frameCounter) + " fps"; 
             frameCounter = 0; 
             start = System.currentTimeMillis(); 
             ovl = makeFpsOverlay(overlayPaint, fps); 
            } 


            Random r = new Random(); 
            int iterator = r.nextInt(); 
            String mPath = Environment.getExternalStorageDirectory().toString() + "/screenshots/"; 
            File imageFile = new File(mPath); 
            imageFile.mkdirs(); 
            imageFile = new File(imageFile + "/" + iterator + "_screenshot.png"); 
            try { 
             ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
             bm.compress(Bitmap.CompressFormat.PNG, 100, bos); 
             byte[] bitmapdata = bos.toByteArray(); 

             FileOutputStream fos = new FileOutputStream(imageFile); 
             fos.write(bitmapdata); 
             fos.flush(); 
             fos.close(); 
             return; 
            } catch (FileNotFoundException e) { 
             e.printStackTrace(); 
            } catch (IOException e) { 
             e.printStackTrace(); 
            } 
            return; 
           } 

          } catch (IOException e) { 
           e.getStackTrace(); 
           Log.d(TAG, "catch IOException hit in run", e); 
          } 
         } 

        } finally { 
         if (c != null) { 
          mSurfaceHolder.unlockCanvasAndPost(c); 
         } 

        } 
       } 
      } 
      // return; 
     } 

    } 

    private void init(Context context) { 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 
     thread = new MjpegViewThread(holder, context); 
     setFocusable(true); 
     overlayPaint = new Paint(); 
     overlayPaint.setTextAlign(Paint.Align.LEFT); 
     overlayPaint.setTextSize(12); 
     overlayPaint.setTypeface(Typeface.DEFAULT); 
     overlayTextColor = Color.WHITE; 
     overlayBackgroundColor = Color.BLACK; 
     ovlPos = MjpegView.POSITION_LOWER_RIGHT; 
     displayMode = MjpegView.SIZE_STANDARD; 
     dispWidth = getWidth(); 
     dispHeight = getHeight(); 
    } 

    public void startPlayback() { 
     if (mIn != null) { 
      mRun = true; 
      thread.start(); 
     } 
    } 

    public void stopPlayback() { 
     mRun = false; 
     boolean retry = true; 
     while (retry) { 
      try { 
       thread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
       e.getStackTrace(); 
       Log.d(TAG, "catch IOException hit in stopPlayback", e); 
      } 
     } 
    } 

    public MjpegView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public void surfaceChanged(SurfaceHolder holder, int f, int w, int h) { 
     thread.setSurfaceSize(w, h); 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     surfaceDone = false; 
     stopPlayback(); 
    } 

    public MjpegView(Context context) { 
     super(context); 
     init(context); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     surfaceDone = true; 
    } 

    public void showFps(boolean b) { 
     showFps = b; 
    } 

    public void setSource(MjpegInputStream source) { 
     mIn = source; 
     startPlayback(); 
    } 

    public void setOverlayPaint(Paint p) { 
     overlayPaint = p; 
    } 

    public void setOverlayTextColor(int c) { 
     overlayTextColor = c; 
    } 

    public void setOverlayBackgroundColor(int c) { 
     overlayBackgroundColor = c; 
    } 

    public void setOverlayPosition(int p) { 
     ovlPos = p; 
    } 

    public void setDisplayMode(int s) { 
     displayMode = s; 
    } 
} 

и мои ошибки ....

at android.view.View.performClick(View.java:4424) 
     at android.view.View$PerformClick.run(View.java:18383) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4998) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
     at dalvik.system.NativeStart.main(Native Method) 
     09-09 11:18:47.227 10479-10900/com.example.isec.isec D/dalvikvm﹕     GC_FOR_ALLOC freed 4036K, 22% free 21589K/27336K,    paused 79ms, total 82ms 
     09-09 11:18:47.237 10479-10900/com.example.isec.isec W/System.err﹕                        java.io.FileNotFoundException: /storage/sdcard/screenshots/-1867569916_screenshot.png: open failed: EACCES (Permission denied) 
     09-09 11:18:47.237 10479-10900/com.example.isec.isec W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409) 
     09-09 11:18:47.237 10479-10900/com.example.isec.isec W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
     09-09 11:18:47.237 10479-10900/com.example.isec.isec W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73) 
     09-09 11:18:47.247 10479-10900/com.example.isec.isec W/System.err﹕ at com.examp 

ответ

1

Вам необходимо включить следующую строку в Android Manifest

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

я уже включен, что разрешение , У меня все еще есть такие ошибки. –

Смежные вопросы