These two days I played Android Wanji Tian did not move, his mind springing a note, would like to take pictures with the camera Android call (photographed), and then survive, the Internet Baidu, a lot of people say, I tried 7.0 the following very easy on the successful, because if not dynamic at 7.0 application for permission to apply for permission dynamic I did not write, I manually open permissions, so it means a waste, interested look at it.
First rejection layout file code:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.paizhao.MainActivity"> <Button android:id="@+id/take_photo" android: text = "camera" android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="0dp" /> </android.support.constraint.ConstraintLayout>
Layout code so it can copy itself, does not matter.
Then initialize MainActivity:
import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Button btn; // camera button private Uri ImageUri; // store pictures Address private ImageView picture; // display the picture with ImageView public static final int TAKE_PHOTO=1; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.take_photo);//加上ID picture = (ImageView) findViewById(R.id.picture); } }
Then there is the more disgusting place
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File outputImage = new File(getExternalCacheDir(), "outputImage.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace (); } if (Build.VERSION.SDK_INT> = 24) {// judge this place Android version is greater than 7.0, if it is greater than 24, then his version is greater than 7.0 ImageUri = FileProvider.getUriForFile (MainActivity.this, "com.example.paizhao.FileProvider", outputImage); // show transition operation, using FileProvider transformation, a file transfer type content type into // Android 7.0 system after the ban applied to the external public file: // URI, so we have to transition. // FileProvider is a subclass of ContentProvider, private transfer files between applications for use prior to registration AndroidManifest } else { Of the geUri = Do it .fromFile (of tpu Image); System.out.println(ImageUri); } Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUri); startActivityForResult(intent, TAKE_PHOTO); } });
display image:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case TAKE_PHOTO: if (resultCode==RESULT_OK){ try { // A picture is taken is displayed Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(ImageUri)); picture.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace (); } } break; default: break; } }
Then is 7.0 or more unique things in the registration FileProvider AndroidManifest.
<provider android:name="android.support.v4.content.FileProvider" android: authorities = "com.example.paizhao.FileProvider" // prevent some students directly copy the code and consequently will not fall collapse, authorities are your package name + FileProvider, FileProvider.getUriForFile above the second argument and it you must be exactly equal. android: exported = "false" // like the rest of the direct copy android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
Add the phrase: Also in AndroidManifest, seemingly use the following 7.0
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <! - call camera permissions -> <uses-permission android:name="android.permission.CAMERA" />
Well, go try it.