2016-04-17 3 views
0

私は自分の携帯電話で最後にインストールしたアプリケーションからアイコン(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 "); 
    } 


} 



} 
+0

これは機能していないようですか? – DeeV

+0

こんにちは@DeeV私が抱えている問題は、.pngがどこに作成されているのかわからないということですか?私は私のディレクトリ全体を検索し、私が作成しているファイル(applicationName.png)が存在しないように見えます。 – Natalie

+0

[パッケージ名からアプリケーションのアイコンを取得するにはどうすればいいですか?](http://stackoverflow.com/questions/17985500/how-can-i-get-the-applications-icon-from-the-package -name) –

答えて

0

Context#openFilesInput()が開くファイルディレクトリはそのContext#getFilesDir()戻って同じプライベートディレクトリです。お使いの携帯電話がルートされていない限り、ADBを介してこのフォルダを開くことはできません。あなたはこのかかわらを行うことによってFileオブジェクトを取得することができます

File imageFile = new File(context.getFilesDir(), "imgName.png");

をそれが現在あなたがどんなApplicationInfo#toString()戻った後の画像を命名していることに、注意することが重要です。代わりにapplicationName + ".pngでファイルを開くことができます。または、これが最新のインストール済みアプリであるため、「latestApp.png」またはそれ以外のものとして保存します。

+0

ありがとうございました!これは私が探していたものです! – Natalie

関連する問題