2016-04-26 61 views
0

我想讓我的android應用程序爲當前正在運行的活動截圖,然後將其保存在解析服務器..但它沒有工作...有誰知道這是什麼問題代碼保存在解析服務器的屏幕截圖

我的代碼...

public class MainActivity extends AppCompatActivity { 

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

     Button b = (Button) findViewById(R.id.button); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       takeScreenshot(); 


      } 
     }); 

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

     try { 
          String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".png"; 

      // 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.PNG, quality, outputStream); 



      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      Uri uri = Uri.fromFile(imageFile); 
      byte[] image = stream.toByteArray(); 

      // Create the ParseFile 
      ParseFile file = new ParseFile(uri.toString(), image); 
      // Upload the image into Parse Cloud 
      file.saveInBackground(); 

      // Create a New Class called "ImageUpload" in Parse 
      ParseObject imgupload = new ParseObject("Image"); 

      // Create a column named "ImageName" and set the string 

      // Create a column named "ImageFile" and insert the image 
      imgupload.put("IMG", file); 

      // Create the class and the columns 
      imgupload.saveInBackground(); 

      // Show a simple toast message 
      Toast.makeText(MainActivity.this, "Image Uploaded", 
        Toast.LENGTH_SHORT).show(); 

      outputStream.flush(); 
      outputStream.close(); 
     } catch (Throwable e) { 
      // Several error may come out with file handling or OOM 
      e.printStackTrace(); 
     } 
    } 
+0

發佈logcat的錯誤,或者我會問你「你試過了什麼?你是否用調試器來做它? – Shark

+0

它告訴我,圖像上傳....但沒有出現在解析 –

回答

1

您應該更改您的代碼

bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream); 
outputStream.flush();// before file usage 
outputStream.close(); 

而且從方法的末尾移除平齊。

+0

感謝您的答覆,但它沒有工作 –

0

的主題解析工作,所以先得到回調ParseImage是救還是不說拿再打響應像...

final ParseFile file = new ParseFile(uri.toString(), image); 
    // Upload the image into Parse Cloud 
    file.saveInBackground(new SaveCallback() { 

     @Override 
     public void done(ParseException e) { 
      // TODO Auto-generated method stub 
      if (e == null) { 
       // success 
       // hear save image 
       // upload image into parse 
       uploadImgae(file); 
      } else { 
       // fail 
      } 
     } 
    }); 

那麼這種方法。

public void uploadImgae(ParseFile file) { 
    // TODO Auto-generated method stub 

    // Create a New Class called "ImageUpload" in 
    // Parse 
    ParseObject imgupload = new ParseObject("Image"); 

    // image 
    imgupload.put("IMG", file); 

    // Create the class and the columns 
    imgupload.saveInBackground(new SaveCallback() { 

     @Override 
     public void done(ParseException e) { 
      // TODO Auto-generated method stub 
      if (e == null) { 
       // success 
      } else { 
       // fail 
      } 
     } 
    }); 
}