レイアウトからビットマップを生成し、ビットマップを内部メモリのファイルに保存する方法を使用しようとしています。ただし、getApplicationContext()は解決されません。ここで getApplicationContext()を解決できません
は、このメソッドを生成するために、StackOverflowのコードからいくつかの助けを使用した方法private void generateAndSaveBitmap(View layout) {
//Generate bitmap
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap imageToSave = layout.getDrawingCache();
//Create a file and path
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File fileName = new File(directory, "sharableImage.jpg");
if (fileName.exists())
fileName.delete();
//Compress and save bitmap under the mentioned fileName
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// return directory.getAbsolutePath();
}
ためのコードです。 getApplicationContext()
で関連するクエリを読んだ後でも、問題を見つけることができません。すべてのヘルプは本当に
EDITをいただければ幸いです。
よろしく
'getApplicationContext()'は 'Context'のメソッドです。あなたのクラスは、直接的または間接的に「文脈」の範囲を広げていますか? buildDrawingCache()を使用する場合は –
です。あなたはまたそれを使用した後にそれを破壊する必要があります – user3802077
@ user3802077ポイントのおかげで。コードに追加 – Traveller