[英]Homescreen shortcuts with icons
我試圖在android上以編程方式創建主屏幕快捷方式。 到目前為止,我已經能夠使用以下代碼添加快捷方式:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.icon);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
但是,使用我的資源中的默認圖標安裝快捷方式。 但是,我想從我的網站上獲取圖標並在快捷方式中添加一個圖標。 首先,我需要下載此快捷方式。 假設我已完成此操作,並且圖標位於SD卡上,我無法設置可繪制圖標。
以下代碼:
try {
Uri contentURI = Uri.parse("http://mnt/sdcard/mytest/test.png");
ContentResolver cr = mContext.getContentResolver();
InputStream in;
in = cr.openInputStream(contentURI);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
Bitmap thumb = BitmapFactory.decodeStream(in,null,options);
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, thumb);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
該文件肯定存在,我已經驗證使用adb shell ...這段代碼顯示以下錯誤:
10-13 16:11:31.184: WARN/System.err(23273): java.io.FileNotFoundException: No content provider: /mnt/sdcard/mytest/test.png
我究竟做錯了什么?
謝謝
您的應用程序似乎無法訪問test.png。 確保它存在。 也許你可以從本地存儲而不是SD卡開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.