android在主程序中调用图片,009android初级篇之APP中使用系统相机相册等集成应用...
009android初級篇之APP中使用系統相機相冊等集成應用
android應用中使用相機功能,大致有兩種方式實現:
直接調用系統內部的相機程序,顯示的也是系統預設的界面(簡單,只有簡單的拍照功能);
自己去implement一個相機程序(不難,較具備彈性,但相對復雜);
權限
如果需要拍照功能,則需要在AndroidManifest.xml文件中添加權限:
調用系統相機應用
這是第一種方式
在啟動相機前先指定好圖片的文件位置,通知intent,同時也保留在成員變量中。然后在函數中,可以直接打開該文件
private static final int CAMERA_REQUESTCODE=1;
String sFileFullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.jpg";
Log.e("onNavi","file: "+sFileFullPath);
File file = new File(sFileFullPath);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUESTCODE);
獲取返回值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUESTCODE) {
if (resultCode == RESULT_OK) {
Bitmap bmPhoto = (Bitmap) data.getExtras().get("data");
// You can set bitmap to ImageView here 這里可以獲得相片的縮略圖
}
}
}
第二種方式:自定制camera
參考鏈接, 該功能我未實現
Android 自定義camera
同樣的方法可以調用系統相冊
private static final int REQUESTCODE_PICK=2;
Intent mIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(mIntent,REQUESTCODE_PICK);
在onActivityResult中獲得選擇的圖片
if(requestCode == REQUESTCODE_PICK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.e(TAG,"Select image "+picturePath);
}
Intent常用的ACTION
1. Intent.Action_CALL
android.intent.action.CALL
呼叫指定的電話號碼。
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086");
startActivity(intent);
2.Intent.Action.DIAL
String: action.intent.action.DIAL
調用撥號面板
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086");
startActivity(intent);
3.Intent.Action.ALL_APPS
String: andriod.intent.action.ALL_APPS
列出所有的應用。
4. Intent.ACTION_CALL_PRIVILEGED
String:android.intent.action.CALL_PRIVILEGED
調用skype的action
Intent intent = newIntent("android.intent.action.CALL_PRIVILEGED");
intent.setClassName("com.skype.raider",
"com.skype.raider.Main");
intent.setData(Uri.parse("tel:" + phone));
startActivity(intent);
5. Intent.Action_CALL_BUTTON
String: android.action.intent.CALL_BUTTON.
相當于按“撥號”鍵。
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
6. Telephony.SMS_RECEIVED
String: android.provider.Telephony.SMS_RECEIVED
接收短信的action
7. Intent.ACTION_GET_CONTENT
String: android.intent.action.GET_CONTENT
允許用戶選擇特殊種類的數據,并返回(特殊種類的數據:照一張相片或錄一段音)
8. Intent.ACTION_BATTERY_LOW;
String: android.intent.action.BATTERY_LOW
表示電池電量低
9. Intent.ACTION_SEND
String: android.intent.action.Send
發送郵件的action
10. Intent.ACTION_CLOSE_SYSTEM_DIALOGS
當屏幕超時進行鎖屏時,當用戶按下電源按鈕,長按或短按(不管有沒跳出話框),進行鎖屏時,android系統都會廣播此Action消息
11. Intent.ACTION_MAIN
String: android.intent.action.MAIN
標識Activity為一個程序的開始。
12. Intent.ACTION_POWER_CONNECTED;
插上外部電源時發出的廣播
13 Intent.ACTION_POWER_DISCONNECTED;
已斷開外部電源連接時發出的廣播
14.Intent.ACTION_ANSWER
Stirng:android.intent.action.ANSWER
處理呼入的電話。
15 .Intent.ACTION_BUG_REPORT
String: android.intent.action.BUG_REPORT
顯示Dug報告。
16. android.intent.action.MAIN
決定應用程序最先啟動的Activity
17.android.intent.category.LAUNCHER
決定應用程序是否顯示在程序列表里
如果只有一個activity的應用程序只聲明了 android.intent.action.MAIN ,沒有聲明 android.intent.category.LAUNCHER,eclipse運行是將報錯,桌面也不會有圖標。
如果存在多個activity都聲明了android.intent.action.MAIN與android.intent.category.LAUNCHER。將會有多個圖標在桌面上。
action的操作有很多,需要的話,繼續百度。
參考鏈接
總結
以上是生活随笔為你收集整理的android在主程序中调用图片,009android初级篇之APP中使用系统相机相册等集成应用...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说下我常用的几个精简版系统的感受
- 下一篇: java 域的隐藏_Windows Se