-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="edu.hzu.myapplication"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<provider | ||
android:name="android.support.v4.content.FileProvider" | ||
android:authorities="com.example.cameraalbumtest.fileprovider" | ||
android:exported="false" | ||
android:grantUriPermissions="true"> | ||
<meta-data | ||
android:name="android.support.FILE_PROVIDER_PATHS" | ||
android:resource="@xml/file_paths"/> | ||
</provider> | ||
</application> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | ||
<uses-feature android:name="android.hardware.camera" | ||
android:required="false" /> | ||
|
||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package edu.hzu.myapplication; | ||
|
||
import android.Manifest; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.provider.MediaStore; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.FileProvider; | ||
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 java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
public static final int T_P=1; | ||
private ImageView picture; | ||
private Uri imageUri; | ||
private static String[] PERMISSIONS_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
permission(); | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
Button takephoto=(Button) findViewById(R.id.take_p); | ||
picture=(ImageView)findViewById(R.id.picture); | ||
|
||
takephoto.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
File outputImage=new File(getExternalCacheDir(),"output_image.jpg"); | ||
try{ | ||
if(outputImage.exists()){ | ||
outputImage.delete(); | ||
} | ||
outputImage.createNewFile(); | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} | ||
if(Build.VERSION.SDK_INT>=24){ | ||
imageUri= FileProvider.getUriForFile(MainActivity.this,"com.example.cameraalbumtest.fileprovider",outputImage); | ||
} | ||
else{ | ||
imageUri=Uri.fromFile(outputImage); | ||
} | ||
//启动照相机 | ||
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); | ||
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); | ||
startActivityForResult(intent,T_P); | ||
} | ||
|
||
}); | ||
} | ||
private void permission(){ | ||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { | ||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 1); | ||
} | ||
} | ||
} | ||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
if (requestCode == 1) { | ||
for (int i = 0; i < permissions.length; i++) { | ||
Log.d("权限动态申请", "申请的权限为:" + permissions[i] + ",申请结果:" + grantResults[i]); | ||
} | ||
} | ||
} | ||
@Override | ||
protected void onActivityResult(int requestCode,int resultCode,Intent data){ | ||
switch (requestCode){ | ||
case T_P: | ||
if(resultCode==RESULT_OK){ | ||
try{ | ||
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); | ||
picture.setImageBitmap(bitmap); | ||
}catch (FileNotFoundException e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
break; | ||
default:break; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout 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" | ||
android:orientation="vertical" | ||
tools:context=".MainActivity"> | ||
|
||
<Button | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="拍照" | ||
android:id="@+id/take_p"/> | ||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/picture" | ||
android:layout_gravity="center_horizontal"/> | ||
|
||
</LinearLayout> |