-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