- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我从图像选择器中选择图像时,出现此错误。直到我开始在我的应用程序中使用权限,我才明白。这是我的 SDK 版本:
compileSdkVersion 27
buildToolsVersion "27.0.3"
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
all*.exclude group: 'com.android.support', module: 'support-annotations'
compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
}
defaultConfig {
applicationId "com.myapp"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
ndk {
abiFilters "armeabi-v7a", "x86"
}
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries = false
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.github.hotchemi:permissionsdispatcher:4.0.0-alpha1"
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:4.0.0-alpha1"
implementation 'com.android.support:support-v13:27+'
implementation 'com.android.support:appcompat-v7:27+'
implementation "com.facebook.react:react-native:+" // From node_modules
}
我阅读了其他问题来帮助我解决这个问题,并找到了这个权限的 java 代码:
private static final int PICK_FROM_GALLERY = 1;
ChoosePhoto.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v){
try {
if (ActivityCompat.checkSelfPermission(EditProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(EditProfileActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
} else {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
switch (requestCode) {
case PICK_FROM_GALLERY:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
} else {
//do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
}
break;
}
}
我把它放在我的类中 mainactivity.java
文件并收到此错误:error: <identifier> expected
.我不确定这是否是修复权限错误的解决方案。
ChoosePhoto.setOnClickListener(new View.OnClickListener()
堆栈跟踪:
07-22 17:59:03.978 8497 8497 D ViewRootImpl@39eadf9[UCropActivity]: MSG_WINDOW_FOCUS_CHANGED 0
07-22 17:59:03.992 8497 8497 E BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/IMMQY/IMG_20180722175858_942.jpg (No such file or directory)
07-22 17:59:03.996 8497 8497 W System.err: java.lang.Exception: Invalid image selected
native 代码:
componentDidMount(){
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
'title': 'Cool Photo App Camera Permission',
'message': 'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the camera")
} else {
console.log("Camera permission denied")
}
} catch (err) {
console.warn(err)
}
}
}
最佳答案
有两件事 1. 您需要在 list 中添加外部读取存储的权限,然后才能使用它,如果您使用 23 以上的 api,则必须使用 Easy 权限。
写:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
阅读:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
23 岁以上:
private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
pickImageFromGallery();
} else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}
您仍然可以通过 InputStream (ContentResolver#openInputStream(Uri uri)) 或通过文件描述符访问文件内容。
这也适用于旧的 android 版本
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
decodeUri(data.getData());
}
}
public void decodeUri(Uri uri) {
ParcelFileDescriptor parcelFD = null;
try {
parcelFD = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor imageSource = parcelFD.getFileDescriptor();
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(imageSource, null, o);
// the new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);
imageview.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// handle errors
} catch (IOException e) {
// handle errors
} finally {
if (parcelFD != null)
try {
parcelFD.close();
} catch (IOException e) {
// ignored
}
}
}
希望对你有帮助
关于java - React Native 的 BitmapFactory : Unable to decode stream: java. io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455690/
通过使用方法 BitmapFactory.decodeStream(Resources res,int id) 和 BitmapFactory.decodeFile(String pathName)
我的应用程序从 SD 卡解码大量位图,因此我想重用现有位图以减少 GC 工作。我看到来自 Android Training 的示例和 this video它非常适合 BitmapFactory.dec
哪种设置 ImageView 图像的方法更好(使用更少的堆空间)? imageView.setImageDrawable(Drawable.createFromPath(path)); 或
我在向 gridview 添加可变背景时遇到 OutOfMemoryError...在 final Bitmap shelfBackground = BitmapFactory.decodeResou
url="http://www.nasa.gov/sites/default/files/styles/946xvariable_height/public/ladee_spin_2_in_motio
我打算编写一个小画廊应用程序。所以我有一个带有图像的 gridview,显示的图像被存储在本地设备上。我得到的是一个类 ImageLoader,它在后台线程 (AsyncTask) 中加载特定路径中的
在我的应用程序中,我有一个文件: private File TEMP_PHOTO_FILE = new File(Environment.getExternalStorageDirectory(),
您好,我正在从名为 image.png 的 png 图像创建位图。图像的尺寸为 75(宽)x 92(高)。当我运行这段代码时: Bitmap bitmap = BitmapFactory.decode
在我的 android 应用程序中,我上传和下载 JPEG 图像。对于某些图像,BitmapFactory 对下载的图像进行下采样,我不明白为什么以及如何阻止它这样做。我在上传和下载图像时记录位图对象
我有同一张图片的两份副本。其中一个在应用程序的资源文件夹(默认可绘制对象)中,另一个在外部存储中。 我用以下代码得到了位图: // Get from storage BitmapFactory.d
嘿,我不确定为什么每次我在图库中选择图片时都会出现这个问题? 代码如下: if (v == uploadImageButton) { // below
今天我的应用程序崩溃了,出现内存不足错误。 java.lang.OutOfMemoryError in android.graphics.BitmapFactory.nativeDecodeAsset
我正在尝试从Camera.PictureCallback onPictureTaken获取Jpeg,但是当我这样做时 bitmapImage = BitmapFactory.decodeByteArr
在研究 Android 的 BitmapFactory.Options 类时,我注意到它的字段是公开可以访问和修改的。 这与一般封装规则相反,该规则规定字段应声明为 private,并且应通过 pub
这段代码给出了运行时异常,我无法捕获它。 BitmapFactory.decodeByteArray 不会返回 null 或类似的内容。 Bitmap bitmap = BitmapFactory.d
我有一个包含两列的 ArrayList,并显示下面带有文本的图像。我正在使用高质量图像,并且需要在 GridView 中以良好的质量显示这些图像,为此我使用 BitmapFactory.Options
我应该从互联网获取图像并将其显示在我的 Activity 中。为此,我使用 asynctask 类。我忘记在我的 list 文件中提及互联网权限,并且应用程序成功运行,没有给出任何异常,并且能够获取模
如何从字节数组构造原始大小的位图? byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); Bitma
我在这里从 Cursor 获取值: if (mProfileCursor.moveToFirst()) { byte[] blob = mProfileCursor.getBl
我正在尝试使用这个: BitmapFactory.decodeFile("logo.jpg") 与我的 apk 相比,我似乎没有将文件:logo.jpg 放在正确的位置。它应该去哪里? 附言我得到的错
我是一名优秀的程序员,十分优秀!