2016-04-26 9 views
0

Ich versuche, meine Android-App einen Screenshot für die aktuelle laufende Aktivität machen und dann in Parse-Server speichern .. aber es hat nicht funktioniert ... weiß jemand, was ist falsch mit diesemScreenshot im Parse-Server speichern

Code

meinen Code ...

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

Veröffentlichen Sie die Logcat mit dem Fehler, oder ich frage Sie nur "Was haben Sie versucht? Haben Sie es mit einem Debugger vielleicht Schritt?" – Shark

+0

es sagt mir, dass das Bild hochgeladen wird .... aber nichts erscheint in Parse –

Antwort

1

sollten Sie Ihren Code ändern zu

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

Und von Ende Methode bündig entfernen.

+0

Danke für Ihre Antwort, aber es hat nicht funktioniert –

0

Parse Arbeiten am Thema so erhalten zuerst ParseImage Rückruf ist, dass speichern oder nicht nehmen Rückruf Antwort wie ...

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 
      } 
     } 
    }); 

dann diese Methode.

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 
      } 
     } 
    }); 
} 
Verwandte Themen