2016-04-26 5 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(); 

にコードを変更し、メソッドの最後からフラッシュを削除する必要があります。その最初のバックParseImageに呼んでもらう

+0

返信ありがとうございますが動作しませんでした –

0

解析作業スレッドでは...のような応答コールバック取る保存するかではないこと、その後

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 
      } 
     } 
    }); 
} 
関連する問題