wan*_*mer 6 android redmi-device xiaomi
我在 S/O 上看到了与此相关的其他几个问题,但与我的问题最接近的问题似乎没有得到很多答复(小米 MI 设备未从图库中选取图像)。希望这个问题能有更好的运气。
我正在尝试从手机图库中选择图像,并将图像路径传递到另一个活动,以便用户预览该图像。
我已经在另外两个设备(Moto E 和名为 Coolpad 的设备?)上对此进行了测试,它们似乎都工作得很好。
(调试 Android 源代码似乎不是一个实用的选择。)
在主活动中,在 UI 触发器上,我使用以下代码启动图库选择器:
private void dispatchPickPictureIntent() {
Intent pickPictureIntent = new Intent(Intent.ACTION_PICK);
pickPictureIntent.setType("image/*");
startActivityForResult(pickPictureIntent, REQUEST_IMAGE_PICK);
}
Run Code Online (Sandbox Code Playgroud)
我这样处理结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
mCurrentPhotoPath = getRealPathFromURI(this, selectedImage);
launchUploadActivity();
}
}
private String getRealPathFromURI(Context context, Uri uri) {
Cursor cursor = null;
try {
String [] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null)
cursor.close();
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我将文件路径存储在全局变量中,mCurrentPhotoPath就会调用以下方法:
private void launchUploadActivity() {
Intent intent = new Intent(HomeActivity.this, UploadActivity.class);
intent.putExtra(getString(R.string.photo_path), mCurrentPhotoPath);
startActivityForResult(intent, REQUEST_IMAGE_UPLOAD);
}
Run Code Online (Sandbox Code Playgroud)
即使在红米手机上,到目前为止,一切都运行顺利。在UploadActivity的onCreate方法中,我成功收到图像文件路径字符串。
但是,然后,我尝试预览图像:
private void previewPhoto() {
imageView.setVisibility(View.VISIBLE);
BitmapFactory.Options options = new BitmapFactory.Options();
// Avoid OutOfMemory Exception
options.inSampleSize = 8;
// This line returns a null, only on the Xiaomi device
final Bitmap bitmap = BitmapFactory.decodeFile(photopath, options);
imageView.setImageBitmap(bitmap);
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试调试这个问题,但是一旦我进入 BitmapFactory 的源代码,我就遇到了 Android Studio 看似未解决的问题(/sf/answers/2839652161/),这使得它毫无用处。
任何有关我如何从这里继续进行的指示将不胜感激。
因此,我能够将问题范围缩小到不正确的文件路径。Uri 到文件路径函数并未在所有设备上获得所需的结果。
我使用了这里建议的方法:https ://stackoverflow.com/a/7265235/3438497
并对代码进行了一些调整,以便在从图库中选取图像时,我直接使用图像的 Uri。
| 归档时间: |
|
| 查看次数: |
6219 次 |
| 最近记录: |