我的应用程序有两个问题。第一个问题是,我得到了一个ioException:权限被拒绝,即使我认为我已经拥有了所需的所有权限。
当我运行photoFile=createImageFile()时,dispatchTakePictureIntent方法会触发权限错误。
public class MainActivity extends AppCompatActivity {
EncodeToBase64 encode = new EncodeToBase64();
String lastPhotoAsBase64;
ImageView mImageView;
ImageView mBackground;
private static int RESULT = 1;
static final int REQUEST_TAKE_PHOTO = 1;
static final int PICK_IMAGE_REQUEST = 2;
String mCurrentPhotoPath;
TextView textView;
ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT);
}
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.imageView);
mBackground = findViewById(R.id.backgroundImage);
Button cameraButton = findViewById(R.id.cameraButton);
Button galleryButton = findViewById(R.id.galleryButton);
textView = findViewById(R.id.textView);
progress = findViewById(R.id.progressBar);
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try {
setPic();
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == PICK_IMAGE_REQUEST && data.getData() != null) {
Uri selectedImage = data.getData();
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
mCurrentPhotoPath = picturePath;
cursor.close();
setPic();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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 = getExternalStoragePublicDirectory(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();
galleryAddPic();
return image;
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
public void setPic() throws IOException {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
//BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
//int deg = rotationInDegrees;
Matrix matrix = new Matrix();
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
lastPhotoAsBase64 = encode.encode(bitmap);
if (lastPhotoAsBase64 != null) {
new postData().execute(lastPhotoAsBase64);
} else {
Toast.makeText(MainActivity.this, "No base64 data found!", Toast.LENGTH_LONG).show();
}
mImageView.setImageBitmap(bitmap);
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
public class postData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
HttpConnectionActivity hh = new HttpConnectionActivity();
String temp = hh.HttpConnection(strings[0]);
return temp;
}
@Override
protected void onPreExecute() {
textView.setVisibility(View.INVISIBLE);
progress.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
progress.setVisibility(View.INVISIBLE);
JSONObject jsonRaw = null;
String age = null;
String gender = null;
String result = null;
if(!(s.contains("5002"))) {
try {
jsonRaw = new JSONObject(s);
age = jsonRaw.getJSONArray("images").getJSONObject(0).getJSONArray("faces").getJSONObject(0).getJSONObject("attributes").getString("age");
gender = jsonRaw.getJSONArray("images").getJSONObject(0).getJSONArray("faces").getJSONObject(0).getJSONObject("attributes").getJSONObject("gender").getString("type");
result = "Du är en " + ((gender.equals("M")) ? "Man" : "Kvinna") + " som är " + age + " år gammal";
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
result = "Inga ansikten hittades";
}
textView.setText(result);
textView.setVisibility(View.VISIBLE);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cliff.camera2">
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<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.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="DCIM/SkitApp" />
</paths>
CAMERA权限是一个危险的权限,write_external_storage也是。从API23开始,您必须在运行时请求这些权限。这意味着即使您在清单中列出了它,对于API>=23,您必须请求它们
我正试图让我的应用程序为新的Android M权限更改做好准备,发现了一些奇怪的行为。我的应用程序使用相机意图机制,允许用户从相机中获得一张照片。但是在另一个活动中,需要使用具有camera权限的camera本身(因为库依赖项card.io需要这样做)。 这是我用来让用户用相机点击一张照片和/或选择一张图像的代码 我在活动中单击按钮时调用。当我没有相机权限在我的应用程序,它的工作很好,但与添加,我
我目前的理解是Spring没有区分角色和权威,唯一的区别是角色名称的缩写。 我希望实现的是具有权威的角色。另外,如果我给一个用户ADMIN这个角色,他就会获得所有相关的特权(举个例子,我们可以访问网站上的路由以更新页面) 我当前的实现如下:我有一个自定义特权类,它与Roles类有多对多的关系,而Roles类与用户的类有多对多的关系。当,它迭代所有权限并获取。 我的问题是:有没有更好的方法(最好是s
本文向大家介绍Android 相机相册权限设置方法,包括了Android 相机相册权限设置方法的使用技巧和注意事项,需要的朋友参考一下 在AndroidManifest.xml中设置相机和相册的权限, 以上这篇Android 相机相册权限设置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。
问题内容: 我已经在应用程序中实现了WKWebView。在显示的网页中有一个文件输入,它应该从照片中导入图像。每当我按该输入并选择“拍摄照片”或“照片库”时,应用程序都会突然崩溃,我认为这是因为该应用程序缺少拍摄照片或从图库中导入的权限。 当用户选择上述方法之一(拍摄照片或照片库)时,如何推送许可请求? 我将Swift 3.0与WKWebView一起使用。 问题答案: 您必须在Info.plist
本文向大家介绍浅谈Android 照相机权限的声明,包括了浅谈Android 照相机权限的声明的使用技巧和注意事项,需要的朋友参考一下 最近写项目,发现在AndroidManifest.xml中声明了调用相机权限之后,打开app无法启动相机,经过一番搜查发现: 问题在于当写项目所使用的API的版本过高时(比如我所用的测试机为android 5.0,而我写这个工程所用的API为27,即android
我知道关于这个话题已经有了一些答案,但我不太清楚如何测量相机和物体之间的距离。 我的目标是: 我用OpenCV通过颜色检测成功地追踪了一个红色的球。现在我试着在红球的中间点一个激光。当红球移动时,激光器应始终跟随红球。我用一个小伺服电机来转动激光。 我在想,如果我能测量物体和相机之间的距离,我就能计算出伺服需要转动的角度。。。 我试图跟踪卡梅伦·洛厄尔·帕尔默的帖子。 我所做的: 我校准了我的Pi