我正在处理应用程序,其中用户可以拍摄照片并将其放入recyclerview networkImageview,如下所示。我正在拍照,但这张照片没有分配给networkImage。未找到拍摄的图像,FileNotFoundException
下面一行抛出一个异常
InputStream is = context.getContentResolver().openInputStream(uri);
java.io.FileNotFoundException:没有内容提供商: /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;
}
}
RecylerViewAdapter
@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);
}
首先,重要的是要考虑'onActivityResult'的实现方式与从相机拍摄的照片以及从照片库拍摄照片的情况下不同。对于Android 6设备(如果您的目标是API 23),还要小心权限管理。从相机拍照意味着你需要存储它。如果您不明确处理权限,则总是会得到“权限被拒绝”。 – thetonrifles