我无法获取FileOutputStream的图像文件路径。存在与权限相关的错误。我该如何解决这个问题?
if (getIntent().getExtras().getString("file_path") != null) {
Log.d(TAG, "Path is NOT NULL");
file = new File(getIntent().getExtras().getString("file_path"));
Bitmap src;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), options);
final int REQUIRED_SIZE = 400;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
src = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
FileOutputStream os;
try{
ExifInterface exif = null;
try {
exif = new ExifInterface(file.getPath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bmRotated = rotateBitmap(src, orientation);
os = new FileOutputStream(file.getAbsoluteFile());
bmRotated.compress(Bitmap.CompressFormat.JPEG, 100, os);
photoView.setImageBitmap(bmRotated);
os.flush();
os.close();
} catch (Exception e){
e.printStackTrace();
}
} else {
Log.d(TAG, "Path is NULL");
}
我试图从其他来源看,但它并没有帮助我知道答案。
答案 0 :(得分:0)
你有没有在清单中宣布这个?我认为你缺少Android许可。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
在AndroidManifest
中声明权限时,您还需要处理Runtime permissions
答案 2 :(得分:0)
将以下权限授予android清单文件.. 如果你执行读写,那么添加两个..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
然后在执行读取和写入操作时执行活动,该时间处理运行时权限,如下所示。
/**
* this method check permission and return current state of permission need.
*/
private boolean checkPermissions() {
int permissionState = ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
return permissionState == PackageManager.PERMISSION_GRANTED;
}
/**
* this method request to permission asked.
*/
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if (shouldProvideRationale) {
} else {
Log.i("Error", "Requesting permission");
// previously and checked "Never ask again".
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
然后执行您的操作。
if (!checkPermissions()) {
requestPermissions();
} else {
// here your operation
getFileDetails();
}
当许可是授权时,然后在下面的方法中回电..
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i("Error", "User interaction was cancelled.");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted. Kick off the process of building and connecting
// GoogleApiClient.
} else {
}
}
}