如何在以下代码中初始化变量位图?
How can I initialise the variable bitmap in the following code?
我知道 Stack Overflow 上有这样的问题
但答案在我的代码中不起作用。
我也尝试过使用 decodeStream()
来获得
一个位图,但我得到了相同的结果。
唯一有效的方法是 decodeResource()
。
我不能使用 decodeResourse
因为我需要
解码我将得到的不断变化的图像
在运行时,屏幕截图
以下所有代码都在服务的onstartCommand()
方法中
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();
您确定您的应用程序清单 (main/AndroidManifest.xml) 包含 android.permission.READ_EXTERNAL_STORAGE 并且您已在 phone 上授予此权限吗?
在我的旧(root)phone 上,示例应用程序在没有此权限的情况下运行。在我较新的、没有根目录的 phone 上,我必须包含此权限才能使应用程序正常工作。
我知道 Stack Overflow 上有这样的问题 但答案在我的代码中不起作用。
我也尝试过使用 decodeStream()
来获得
一个位图,但我得到了相同的结果。
唯一有效的方法是 decodeResource()
。
我不能使用 decodeResourse
因为我需要
解码我将得到的不断变化的图像
在运行时,屏幕截图
以下所有代码都在服务的onstartCommand()
方法中
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();
您确定您的应用程序清单 (main/AndroidManifest.xml) 包含 android.permission.READ_EXTERNAL_STORAGE 并且您已在 phone 上授予此权限吗?
在我的旧(root)phone 上,示例应用程序在没有此权限的情况下运行。在我较新的、没有根目录的 phone 上,我必须包含此权限才能使应用程序正常工作。