アプリケーションの画面キャプチャを作成しようとしていますが、すべてうまくいきましたが、キャプチャの結果は空のファイルです。アプリケーションが空でないファイルを正しく返すようにするにはどうすればよいですか。助けてください。ありがとうございます。
ここに私のコードがあります:
MainActivity クラス:
public class MainActivity extends Activity{
Utilities u = new Utilities();
Context context;
Handler ssHandler = new Handler();
File input = new File(u.getExternalDirectory()+"/BootManager/screenshot.bmp");
private String ssDir = u.getExternalDirectory()+"/DCIM/screens";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
File dcim = new File(u.getExternalDirectory()+"/DCIM");
if(!dcim.exists())
dcim.mkdir();
File screens = new File(dcim+"/screens");
if(!screens.exists())
screens.mkdir();
u.execCommand(context.getFilesDir().getAbsolutePath()+"/busybox chmod 777 /dev/graphics/fb0");
takeScreenshot(3);
finish();
}
void takeScreenshot(final int delay)
{
ssHandler.postDelayed(new Runnable()
{
public void run()
{
takescreenshot();
}
}, delay * 1000);
}
private void takescreenshot(){
if(input.exists())
input.delete();
u.execCommand(context.getFilesDir().getAbsolutePath()+"/bootmanagerSS tryScreenshotClient");
/*if(!input.exists()){
@SuppressWarnings("unused")
int i = ssl.takeScreenShot("/dev/graphics/fb0");
}
*/
converttoPNG();
Toast.makeText( MainActivity.this, "screenshot_saved", Toast.LENGTH_LONG).show();
}
private void converttoPNG() {
Bitmap bmp = null;
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
int minute = c.get(Calendar.MINUTE);
String minutes = String.valueOf(minute);
if(minutes.length() < 2){
minutes = "0"+minutes;
}
String sDate = month + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.YEAR) + "-" + c.get(Calendar.HOUR_OF_DAY) + "." + minutes;
try {
bmp = BitmapFactory.decodeStream(new FileInputStream(input));
FileOutputStream fout = new FileOutputStream(ssDir+"/"+sDate+".jpg");
bmp.compress(CompressFormat.JPEG, 100, fout);
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
input.delete();
startMediaScanner(ssDir+"/"+sDate+".jpg");
}
private void startMediaScanner(String addedPicture)
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ addedPicture)));
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Utilities クラスの execCommand 関数:
public Boolean execCommand(String command)
{
Process process;
try {
Runtime rt = Runtime.getRuntime();
process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
os.close();
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}