In your APP, you often get pictures from your mobile camera and gallery. The encapsulation method here is convenient for you. It solves the acquisition method of Android 7.0 resource uri. It is compatible with machines above SDK19, and has the authority to request integration. Welcome to use it off the shelf, and comment and guide it. Program running results:
Program application class code:
package jason.com.carmerphoto;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import static jason.com.carmerphoto.GetPicUtil.REQUEST_PERMISSION_CAMERA;
import static jason.com.carmerphoto.GetPicUtil.REQUEST_PERMISSION_WRITE;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//control
private ImageView img_album;
private Button btn_dialog;
private TextView tv_desd;
String imagePath = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
this.img_album = (ImageView) findViewById(R.id.img_album);
this.btn_dialog = (Button) findViewById(R.id.btn_pic);
this.tv_desd = (TextView) findViewById(R.id.tv_desc);
this.btn_dialog.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Get content after selecting photos to be compatible with Android = < 19, > 19
this.imagePath = GetPicUtil.handlePicResult(this, data, requestCode, resultCode, img_album);
Log.v("TAG", "get_picpath=" + imagePath);
}
/**
* Do not handle the callback of permission. It is the first time to get permission. Click the control again to trigger
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSION_CAMERA:
//Judge whether there is authority
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
GetPicUtil.take_photo(this);//Turn on the camera.
} else {
Toast.makeText(this, "You need permission!", Toast.LENGTH_LONG).show();
}
break;
case REQUEST_PERMISSION_WRITE:
//Judge whether there is authority
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
GetPicUtil.openAlbum(this);//Open Album
} else {
Toast.makeText(this, "You need permission!", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
tv_desd.setText("sdk_" + Build.VERSION.SDK_INT);
//1. Pop up dialog box to select camera or gallery
GetPicUtil.showPicDialog(this);
//2. Direct camera
// GetPicUtil.xiangjiClick(this);
//3. Directly enter the library
// GetPicUtil.select_photo(this);
}
}
//You may have different versions of Android studio. Don't worry about opening the project after downloading and decompressing. Open the project folder to modify several files. You can use your local version of gradle. Open the project you have run and replace the following code:
1. Open the build.gradle file of the project, modify and replace it with your original project code statement, and save the file.
//classpath 'com.android.tools.build:gradle:2.2.2'
2. Open the file gradle/warpper/gradle-wrapper.properties to replace and save:
//distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip
3. Open the project app/build.gradle, modify the following tool version, open the project you have run and replace the following code:
//compileSdkVersion 25
//buildToolsVersion "25.0.3"
//compile 'com.android.support:appcompat-v7:25.3.1'
//compile 'com.android.support:design:25.3.1'