我试图让我的Android应用程序截取当前正在运行的活动的屏幕截图,然后将其保存在解析服务器中..但它没有工作...有谁知道什么是' s这段代码错了
我的代码......
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 :(得分:1)
您应该将代码更改为
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
outputStream.flush();// before file usage
outputStream.close();
从方法结束处删除flush。
答案 1 :(得分:0)
Parse Work on Thread所以首先回调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
}
}
});
}