我想拍照并在imageView中显示这张照片,它在api 23和24上不能正常工作我尝试添加文件提供程序这就是我所做的。
我的活动:
public class TestActivity extends AppCompatActivity {
ImageView iv;
private File photoFile;
private String mCurrentPhotoPath;
private static final String TAG = "PHOTO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
iv = (ImageView) findViewById(R.id.iv);
photo();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (mCurrentPhotoPath != null) {
File file = new File(mCurrentPhotoPath);
if(file.exists())
Log.e(TAG , "photo jest");
try {
Bitmap mImageBitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), Uri.parse(mCurrentPhotoPath));
iv.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(TestActivity.this, "Nie udało się zapisać zdjęcia ", Toast.LENGTH_LONG).show();
}
}
}
void photo() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
try {
photoURI = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider",
createImageFile());
} catch (IOException e) {
e.printStackTrace();
}
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}
关于表格我添加了一个文件提供者:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="eltegps.pl.mobileterminal.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
这是我的文件提供者:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/eltegps.pl.mobileterminal/files/Pictures" />
</paths>
当我在logcat上拍照时,我看到了:
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Android/data/eltegps.pl.mobileterminal/files/Pictures/JPEG_20170823_084656_-159214243.jpg
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1141)
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:991)
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at android.content.ContentResolver.openInputStream(ContentResolver.java:711)
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:890)
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at eltegps.pl.mobileterminal.Activity.TestActivity.onActivityResult(TestActivity.java:54)
08-23 08:47:01.298 25695-25695/eltegps.pl.mobileterminal W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:7137)
这里我有一个FileNotFoundException
Bitmap mImageBitmap
=MediaStore.Images.Media.getBitmap(getApplicationContext().
getContentResolver(), Uri.parse(mCurrentPhotoPath));
在第一项活动中,我会检查权限;
private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int cameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
private void logIn(){
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS && grantResults.length == listPermissionsNeeded.size()) {
boolean allGranted = true;
for (int grantedResult : grantResults) {
if (grantedResult != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
}
}
if (allGranted) {
logIn();
}
}
}
答案 0 :(得分:2)
我也面临着这个问题,也许已经晚了,但我希望它能对正在研究我的人有所帮助。
问题出在这行
Bitmap mImageBitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), Uri.parse(mCurrentPhotoPath));
尝试将Uri.parse(mCurrentPhotoPath)
更改为Uri.fromFile(new File(mCurrentPhotoPath))
这是因为Uri.fromFile(new File(mCurrentPhotoPath))
会产生file:///Android/data/eltegps.pl.mobileterminal/files/Pictures/namefile.mp4
之类的东西,就像方法的参数需要一样。
答案 1 :(得分:1)
在存储和读取位图之前,您需要获得写入和读取存储的运行时权限。