
在这一行:BitmapFactory.decodefile(mCurrentPhotopath,bmOptions);它会抛出一个fileNotFound异常.这是Logcat:
01-29 23:56:12.296: E/BitmapFactory(30046): Unable to decode stream: java.io.fileNotFoundException: /file:/storage/emulated/0/Pictures/JPEG_20140129_235544_1090805596.jpg: open Failed: ENOENT (No such file or directory)那是在setPic()中;但是在启动intent时文件会被保存并添加到库中,所以在onActivityResult之前,它应该在那里.你看到有什么问题吗?此代码取自AndroID devloper网站http://developer.android.com/training/camera/photobasics.html
static final int REQUEST_TAKE_PHOTO = 1001; private voID dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the file where the photo should go file photofile = null; try { photofile = createImagefile(); } catch (IOException ex) { // Error occurred while creating the file } // Continue only if the file was successfully created if (photofile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromfile(photofile)); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } } } private voID galleryAddPic() { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_file); file f = new file(mCurrentPhotopath); Uri contentUri = Uri.fromfile(f); mediaScanIntent.setData(contentUri); this.sendbroadcast(mediaScanIntent); }编辑下一个代码块来自onActivityResult:
else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == -1)){ // Uri selectedImage = imageUri; mProfilePicPath = mCurrentPhotopath; mPortraitPhoto = setPic(); TextVIEw tv = (TextVIEw) findVIEwByID(ID.ProfilePicText); tv.setText(mProfilePicPath); //} // } } }catch(Exception ex){ Log.d("shkdghrfb", ex.toString()); } } String mCurrentPhotopath; private Bitmap setPic() { // Get the dimensions of the VIEw // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodefile(mCurrentPhotopath, bmOptions); int photoW = bmOptions.outWIDth; int photoH = bmOptions.outHeight; // Determine how much to scale down the image //int scaleFactor = Math.min(photoW/targetW, photoH/targetH); // Decode the image file into a Bitmap sized to fill the VIEw bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = 5; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodefile(mCurrentPhotopath, bmOptions); //mImageVIEw.setimageBitmap(bitmap); return bitmap; } 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 = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); file image = file.createTempfile( imagefilename, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotopath = "file:" + image.getabsolutePath(); galleryAddPic(); return image; }我添加的清单权限:
<uses-permission androID:name="androID.permission.CAMERA" androID:required="false" /> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />解决方法:
我认为文件名中的“文件”不是正确的文件名所以删除它.更改了mCurrentPhotopath =“file:”image.getabsolutePath(); to mCurrentPhotopath = image.getabsolutePath();.如果这只是一个黑客修复请告诉我.重要的是,这适用于所有兼容的设备.
总结以上是内存溢出为你收集整理的来自Android网站的相机意图不起作用 – Android全部内容,希望文章能够帮你解决来自Android网站的相机意图不起作用 – Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)