gpt4 book ai didi

android - 未找到拍摄的图像,FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 15:44:09 26 4
gpt4 key购买 nike

我正在开发用户可以拍照并将其放入 recyclerview networkImageview 的应用程序,如下所示。我正在拍照,但这张图片没有分配给 networkImage。

下面一行抛出异常

 InputStream is = context.getContentResolver().openInputStream(uri);

java.io.FileNotFoundException: No content provider: /storage/emulated/0/Pictures/JPEG_20160219_113457_-713510024.jpg

MainActivity 类

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
String path = ImageUtil.camPicturePath;
mCurrentItem.setPath(path);
mAdapter.notifyDataSetChanged();
mCurrentItem = null;
mCurrentPosition = -1;
}
}

回收 View 适配器

@Override
public void onBindViewHolder(final ListViewHolder holder, int position) {
PostProductImageLIst item = mItems.get(position);
ImageUtil.getInstance().setSelectedPic(holder.imgViewIcon, item.getPath());
}

ImageUtilClass

public static String camPicturePath;

public static void captureImage(Activity activity) {
File photoFile = null;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
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) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
activity.startActivityForResult(takePictureIntent, 100);
}
}
camPicturePath = (photoFile != null) ? photoFile.getAbsolutePath() : null;
}


private LruCache<String, Bitmap> mMemoryCache;

public void setSelectedPic(CustomNetworkImageView view, String url) {
Context context = view.getContext();
if (!TextUtils.isEmpty(url)) {
if (url.startsWith("http")) {
view.setImageUrl(url, CustomVolleyRequest.getInstance(context).getImageLoader());
} else {
try {
Uri uri = Uri.parse(url);
Bitmap bitmap = mMemoryCache.get(url);
if (bitmap == null) {
InputStream is = context.getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(is);
Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
mMemoryCache.put(url, scaled);
if (is!=null) {
is.close();
}
}
view.setLocalImageBitmap(bitmap);
} catch (Exception ex) {
Log.e("Image", ex.getMessage(), ex);
}
}
} else {
view.setImageUrl("", CustomVolleyRequest.getInstance(view.getContext()).getImageLoader());
}
}
public Bitmap scaleBitmap(Bitmap bitmap) {
int width = WIDTH;
int height = (WIDTH * bitmap.getHeight()) / bitmap.getWidth();
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}

最佳答案

从相机和画廊拍照时都需要小心。首先,您需要正确处理用于选择照片的 Intent。下面我报告了构建它的可能实现。

如果你需要同时从相机和图库中获取照片。

public Intent buildPicturePickerIntent(PackageManager packageManager) {
// Camera
final List<Intent> cameraIntents = new ArrayList<>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
cameraIntents.add(intent);
}
// Filesystem
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
// returning intent
return chooserIntent;
}

重要的是 onActivityResult 的实现必须以不同的方式处理从相机拍摄的照片和从画廊拍摄的照片。您可以在下面看到示例代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_CODE && resultCode == RESULT_OK) {
Uri uri = data.getData();
// photo from gallery?
if (uri != null) {
// yes, photo from gallery
String path = uri.toString();
// use path here
// ...
} else {
// no, photo from camera
Bitmap photo = (Bitmap) data.getExtras().get("data");
// store bitmap (eventually scaling it before)
// in filesystem here and get absolute path
// ...
}
// do your common work here (e.g. notify UI)
}
}

在文件系统上存储 Bitmap 时要注意两点:

  • 当存储一个新的位图时,您将拥有它的绝对路径。当然你可以使用它,但是如果你想以相同的方式管理来自相机的照片和来自画廊的照片,你需要从绝对路径到内容 URI 的转换。您可以在 SO 上找到此问题的许多答案。
  • 如果您的目标是 API 23,则需要明确处理存储权限。否则,当您尝试将从 Android 6 设备上的相机拍摄的位图保存在存储中时,您将收到权限被拒绝的错误。

Here您可以找到从图库/相机中挑选图像的示例代码,并相应地更新回收器 View 项目。

关于android - 未找到拍摄的图像,FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35511210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com