有時朋友圈發一條狀態,想要添加一張照片我們可以直接用相機拍攝,也可以直接在相冊中選取上傳,這就是用到了應用調用相機或者相冊的功能。我們如何爲應用添加這個功能呢?
調用相機拍照
對於使用相機進行拍攝實現的方式有很多種,可以直接在應用中自己定義一個Camera,也可以調用系統的相機。這裏我們只學習使用隱式調用相機的方法。
1. 首先定義佈局,Button用於啓動系統的相機,ImageView用於顯示拍攝的圖片。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_start_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啓動系統相機"/>
</RelativeLayout>
2. 在Activity中通過Intent隱式啓動相機。
public class MainActivity extends AppCompatActivity {
private static final int PICTURE_FROM_CAMERA = 0X32;
private static final int PICTURE_FROM_GALLERY = 0X34;
private Button mButtonStart;//啓動相機的按鈕
private Button mButtonOpen;//啓動相冊的按鈕
private ImageView mImageView;//顯示圖片
private File file;//存儲拍攝圖片的文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonStart = (Button) findViewById(R.id.button_start_camera);
mButtonOpen = (Button) findViewById(R.id.button_open_gallery);
mImageView = (ImageView) findViewById(R.id.imageview);
//點擊按鈕啓動相機
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//啓動相機的Action
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//文件的保存位置
file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//設置圖片拍攝後保存的位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//啓動相機,這裏使用有返回結果的啓動
startActivityForResult(intent, PICTURE_FROM_CAMERA);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICTURE_FROM_CAMERA:
//這裏對圖片進行了壓縮,因爲有些手機拍攝的照片過大,無法顯示到ImageView中,所以我們將圖片近行了壓縮然後在進行顯示
ZipImage.zipImage(Uri.fromFile(file).getPath());
//將圖片設置到ImageView中,這裏使用setImageURI()方法進行設置。
mImageView.setImageURI(Uri.fromFile(file));
break;
}
}
}
}
這裏我們對拍攝獲得的圖片進行了壓縮處理,因爲有些手機拍攝的圖片過大,在ImageView中無法正常顯示(當時在做練習的時候就出現了這個問題),所以需要壓縮一下。這裏我們使用下面的方法進行壓縮,調用ZipImage 的靜態方法zipImage(String savePath)即可。
public class ZipImage {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
3. 這裏使用到了對我們標準SD卡的讀寫操作,所以我們需要加權限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.camerademo" >
<!-- 讀寫標準SD卡的權限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
由於模擬器上的相機是無法使用的,因此我們不在貼出結果。
使用相冊選取
有時候我們想使用相冊裏的圖片,這時我們就需要調用相冊了。我們看下面的步驟……
1. 首先定義佈局,Button用於啓動系統的相冊,ImageView用於顯示選擇的圖片。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_open_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啓動系統相冊"/>
</RelativeLayout>
2. 在Activity中通過Intent隱式啓動系統相冊。
public class MainActivity extends AppCompatActivity {
private static final int PICTURE_FROM_GALLERY = 0X34;
private Button mButtonOpen;//啓動相冊的按鈕
private ImageView mImageView;//顯示圖片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonOpen = (Button) findViewById(R.id.button_open_gallery);
mImageView = (ImageView) findViewById(R.id.imageview);
mButtonOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//設置啓動相冊的Action
intent.setAction(Intent.ACTION_GET_CONTENT);
//設置類型
intent.setType("image/*");
//啓動相冊,這裏使用有返回結果的啓動
startActivityForResult(intent, PICTURE_FROM_GALLERY);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICTURE_FROM_GALLERY:
//通過返回的data數據,獲取圖片的路徑信息,但是這個路徑是Uri的。
Uri uri = data.getData();
//我們要壓縮進行壓縮首先要將Uri地址轉換爲真實路徑。
File file = getFilePath(uri);
//壓縮圖片
ZipImage.zipImage(file.getAbsolutePath());
mImageView.setImageURI(Uri.fromFile(file));
break;
}
}
}
@NonNull
private File getFilePath(Uri uri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
return new File(img_path);
}
}
由於我們通過返回的data數據獲取圖片的路徑信息是Uri的,我們沒辦法應用到壓縮圖片的方法中,我們需要先將這個Uri的路徑轉化爲真實的路徑。這裏我們使用下面這個方法獲得uri的真實路徑:
@NonNull
private File getFilePath(Uri uri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
return new File(img_path);
}
獲得真實路徑後在對其進行壓縮,然後顯示。壓縮的方法在前面已經列出來了,所以這裏不再重複……