2016-10-17 10 views
-1

私はスタックオーバーフロー にそのような質問があることを知っているが、答えは私のコードではうまくいきません。次のコードで可変ビットマップを初期化するにはどうすればよいですか?

decodeStream()を使用して、ビットマップ を取得しようとしましたが、同じ結果が得られます。

有効な唯一の方法はdecodeResource()です。

私は常に私は、実行時に を取得する画像、スクリーンショット

を変更 デコードに必要なので、私は次のコードのすべてが

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()方法である decodeResourseを使用することはできません
+0

img4.pngには "/ system/bin/screencap -p /data/data/com.mycompany.myapp/files/img4.png"という文字列が含まれています(これはファイルに書き込むためです)。この文字列からどんな絵が期待されますか? –

+0

この行( "/ system/bin/screencap -p" + "/ data/data/com")を使用して、イメージが "/data/data/com.mycompany.myapp/files"パスに書き込まれることを確かめます。 .mycompany.myapp/files/img4.png ")。getBytes(" UTF-8 ")。理由を説明しましょう。 suの許可を得たら、その行はスクリーンショットをとり、特定のパスでそれを表示します。私は行( "/ system/bin/screencap -p" + "/storage/emulated/0/myscreenshots/img4.png").getBytes("UTF-8 ")を試して、パスにスクリーンショットを書きました/ storage/emulated/0/myscreenshots。だからこそ私はそのディレクトリを私的な場所に置き換えています。 – John

+0

誰かが助けてください。私はあなたが持っているかもしれない質問に開放されています。このnullPointerExceptionは私にストレスを与えています。 – John

答えて

0

あなたのアプリケーションマニフェスト(main/AndroidManifest.xml)にandroid.permission.READ_EXTERNAL_STORAGEが含まれていて、あなたの携帯電話でこの許可を与えてもよろしいですか?


私の古い(根っからの)電話では、サンプルアプリケーションがこの許可なしで働いていました。私の新しい、根元ではない電話では、アプリケーションを動作させるためにこの許可を含める必要がありました。

関連する問題