简体   繁体   中英

save screenshot in parse server

I'm trying to make my android App take a screenshot for the current running activity and then save it in parse server .. but it didn't work ... does anyone knows what's wrong with this code

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

You should change your code to

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

And remove flush from end of method.

Parse Work on Thread so first get call back to ParseImage is save or not that take call back response like...

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

then this method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM