Android呼叫系統的相機保證照片的質量
阿新 • • 發佈:2018-12-31
package camera.com.example.gzh.camera; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import java.io.File; public class MainActivity extends AppCompatActivity { private ImageView mImageView; private String mpathbit; //設定一個sd卡的路徑存放圖片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.iv); mpathbit= Environment.getExternalStorageDirectory().getPath(); mpathbit=mpathbit+"/"+"tt.png"; } public void check1(View view){ startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),1); } public void check2(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uri=Uri.fromFile(new File(mpathbit)); intent.putExtra(MediaStore.EXTRA_OUTPUT,uri); startActivityForResult(intent,2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==1&&resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); Bitmap bim = (Bitmap) bundle.get("data"); mImageView.setImageBitmap(bim); } if (requestCode==2&&resultCode==RESULT_OK){ Bitmap bitmap = BitmapFactory.decodeFile(mpathbit); mImageView.setImageBitmap(bitmap); } } }
xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/activity_main" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:text="點選進入相機" android:id="@+id/btn1" android:onClick="check1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:text="進入相機高質量圖片" android:id="@+id/btn2" android:onClick="check2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
清單檔案中的許可權
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>