-1
我知道有堆栈溢出 这样的问题,但答案只是不工作在我的代码。如何在下面的代码中初始化变量位图?
我也尝试使用decodeStream()
得到 位图,但我只是得到相同的结果。
唯一可行的方法是decodeResource()
。
我不能使用decodeResourse
因为我需要 解码不断变化的图像,我会得到 在运行时,屏幕截图
所有下面的代码是在服务
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
mNotificationManager.notify(1, notification);
startForeground(1,notification);
String path = "/data/data/com.mycompany.myapp/files";
File f = null;
BitmapFactory.Options options = null;
Bitmap bitmap = null;
try{
//Request Su permission
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
//Open an output stream to a private file to my app
FileOutputStream fos = openFileOutput("img4.png", Context.MODE_PRIVATE);
fos.write(("/system/bin/screencap -p " +
"/data/data/com.mycompany.myapp/files/img4.png").getBytes("UTF-8"));
fos.close();
f=new File(path,"img4.png");
options = new BitmapFactory.Options();
options.inSampleSize = 4;
//decodeFile always returns null yet
//I am sure that the file exsists
bitmap = BitmapFactory.decodeFile(
new File(getFilesDir(),"img4.png").getAbsolutePath(),
options);
os.flush();
os.close();
sh.waitFor();
}catch(Exception e){
e.printStackTrace();
}
//I create and show a toast
Toast.makeText(getApplicationContext(),"Exs: " + f.exists()
//I always get a nullPointerException here
" "+ bitmap.getPixel(0,0),Toast.LENGTH_SHORT).show();
的
onstartCommand()
方法
您的img4.png包含字符串“/ system/bin/screencap -p /data/data/com.mycompany.myapp/files/img4.png”(因为这是您写入文件的内容)。你对这个字符串有什么期望? –
我绝对期望使用这一行将图像写入路径“/data/data/com.mycompany.myapp/files”,(“/ system/bin/screencap -p”+ “/ data/data/com .mycompany.myapp /文件/ img4.png “)。的getBytes(” UTF-8" )。让我解释一下为什么。一旦获得su许可,该行就会拍摄一个屏幕截图并将其放入特定路径。我尝试了这一行(“/ system/bin/screencap -p”+ “/storage/emulated/0/myscreenshots/img4.png").getBytes("UTF-8”),它写了一个截图/存储/模拟/ 0/myscreenshots。这就是为什么我将目录替换到私人位置。 – John
有人请帮助。我接受任何您可能遇到的问题。这个nullPointerException异常强调我。 – John