Android Read Local Gallery and Call Camera to Shoot
- 2021-09-05 01:04:43
- OfStack
This article mainly introduces how to read the pictures of Android local gallery and call Android camera for shooting.
1. Layout
The layout is relatively simple. The layout file of MainActviivty has only two buttons, one is to read the gallery, the other is to open the camera, and the layout of ResultActivity has only one ImageView, which is used to display the selected picture or the taken picture.
2. Read the gallery
Because we need to open Android's own gallery, we need to use Intent, which is mainly used for communication between different Activity. The code is as follows:
// Set the return code: Identify the local gallery
private static final int RESULT_IMAGE=100;
// Settings MIME Code: Indicates image Files in all formats are available
private static final String IMAGE_TYPE="image/*";
// Instantiation Intent, Incoming ACTION_PICK, Represents the value derived from the Item Select from 1 Data return
Intent intent=new Intent(Intent.ACTION_PICK,null);
// Settings Data And Type Property, the former is the URI That represents the system gallery URI, The latter is MIME Code
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_TYPE);
// Start this intent Pointed to Activity
startActivityForResult(intent,RESULT_IMAGE);
3. The camera takes photos
// Set the return code: flag camera shooting
private static final int RESULT_CAMERA=200;
// Settings 1 Temporary path to save the photos taken
private static String TEMP_IMAGE_PATH;
// Get the path
TEMP_IMAGE_PATH= Environment.getExternalStorageDirectory().getPath()+"/temp.png";
// Incoming ACTION_IMAGE_CAPTURE: The action Point 1 Camera app
Intent intent1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create File And get its URI Value
Uri photoUri=Uri.fromFile(new File(TEMP_IMAGE_PATH));
//MediaStore.EXTRA_OUTPUT Is a string "output" That is, the key-value pair is placed in the intent Medium
intent1.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
startActivityForResult(intent1,RESULT_CAMERA);
4. Set the callback method
As we can see from the code in 2. 3, since the startActivityForResult () method is running, we set up a callback method to get the data selected in other applications:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==RESULT_IMAGE&&data!=null){
// Photo album
// Obtaining the currently applied contentResolver Object to query the returned data Data
Cursor cursor=this.getContentResolver().query(data.getData(),null,null,null,null);
// Will cursor The pointer moves to the first row of data
cursor.moveToFirst();
// Gets a field named _data Data of
String imagePath=cursor.getString(cursor.getColumnIndex("_data"));
// Settings 1 A intent
Intent intent=new Intent(MainActivity.this,ResultActvity.class);
// Path to pass in the acquired picture
intent.putExtra("mPicPath",imagePath);
// Destruction cursor Object, releasing resources
cursor.close();
startActivity(intent);
}else if(requestCode==RESULT_CAMERA){
// Camera
Intent intent=new Intent(MainActivity.this,ResultActvity.class);
// Because it was set when taking pictures, 1 Save the path, so put it directly into the path
intent.putExtra("mPicPath",TEMP_IMAGE_PATH);
startActivity(intent);
}
}
}
Step 5 Show pictures
It is relatively simple to show pictures. Get the path in intent in the new Activity, then create an Bitmap and set it to imageView. The code is as follows:
public class ResultActvity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
imageView= (ImageView) findViewById(R.id.image);
Intent intent=getIntent();
String picPath=intent.getStringExtra("mPicPath");
Bitmap bitmap=BitmapFactory.decodeFile(picPath);
imageView.setImageBitmap(bitmap);
}
}
At this point, an demo that reads the local gallery and calls the camera to shoot has been completed.