私は自分の携帯電話で最後にインストールしたアプリケーションからアイコン(png形式)を取得することが可能かどうか疑問に思っていましたか?最後にインストールされたアプリケーションのアイコン用のpngファイルを抽出する方法
私は、最後にインストールしたアプリのパッケージ名、共通名、アイコン(png形式)を抽出するアプリを私の携帯電話に書き込んでいます。私はパッケージ名と共通名を得ましたが、今はアイコンファイルを取得しようとしています。ここで私はこれまでどのようにこれを行うには、Web上で読んでいるコードがありますが、私は何かが不足しているようです。アイコンを抽出するための私のコードはすべて、 "try"ステートメントの中にあります。
public class NewInstallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NewInstallReceiver", "Intent: " + intent.getAction());
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Log.d("PACKAGE NAME","Intent" + ai);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("Application NAME", "Intent: " + applicationName);
// http://www.carbonrider.com/2016/01/01/extract-app-icon-in-android/
try {
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d("ICON BITMAPDRAWABLE", "Intent: " + icon);
BitmapDrawable bitmapIcon = (BitmapDrawable)icon;
Log.d("ICON 11111", "Intent: " + bitmapIcon);
FileOutputStream fosIcon = context.openFileOutput(ai + ".png", Context.MODE_PRIVATE);
Log.d("ICON 22222", "Intent: " + fosIcon);
bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
InputStream inputStream = context.openFileInput(ai + ".png");
Log.d("ICON NAME 33333", "Intent: " + inputStream);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Log.d("ICON bitmap 44444", "Intent: " + bitmap);
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
Log.d("ICON drawable 55555", "Intent: " + drawable);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
catch (Exception e){
Log.d("CATCH", "CATCH ");
}
}
}
これは機能していないようですか? – DeeV
こんにちは@DeeV私が抱えている問題は、.pngがどこに作成されているのかわからないということですか?私は私のディレクトリ全体を検索し、私が作成しているファイル(applicationName.png)が存在しないように見えます。 – Natalie
[パッケージ名からアプリケーションのアイコンを取得するにはどうすればいいですか?](http://stackoverflow.com/questions/17985500/how-can-i-get-the-applications-icon-from-the-package -name) –