简而言之,我有一个我正在研究的应用程序需要能够拍摄图像并上传它们。在上传它们之前,我想在屏幕上显示它们。理想情况下,我应该可以使用图库从手机存储中加载图像,或者拍照并直接上传。
我可以拍摄照片并在没有问题的情况下在ImageView中显示。我可以从库中加载图像,但只有那些从某些外部源下载的图像才会显示在ImageView中。例如,如果我上周用相机拍了一张照片并想用画廊选择它,它就不会加载; ImageView只是空白而没有错误。对于我用相机拍摄的每张图像都是这种情况;如果我尝试使用图库加载它不起作用,但如果我使用它们加载其他图像,他们的工作。我无法弄清楚为什么会这样,所以我会在这里提供一些相关的代码,并希望有人能帮助我。
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);
onActivityResult中的代码,它正在加载图像并尝试显示它:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap imageBitmap = (Bitmap) BitmapFactory.decodeFile(picturePath);
imageview.setImageBitmap(imageBitmap);
答案 0 :(得分:0)
private void chooseImageFromGalery() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Choose Image"), 101);
}
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = data.getData();
try {
photoFile = createImageFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
copyFile(new File(getRealPathFromURI(data.getData())), photoFile);
} catch (IOException e) {
e.printStackTrace();
}
refreshFragmentData();
}
private void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Video.Media.DATA };
Cursor cursor = getActivity().managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(CommonParams.MASTER_STORAGE_PATH + "/" + CommonParams.categorySelected);
File image = File.createTempFile(imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
希望这可以帮到你。
答案 1 :(得分:0)
Intent pickPhoto = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickPhoto.setType("image/*");
startActivityForResult(pickPhoto, 1);
onActivityResult中的代码,它加载图像并尝试显示它:
Intent imageReturnedIntent;
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File mFile = new File(filePath);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bp = BitmapFactory.decodeFile(filePath, options);