
您好我正在尝试保存在我的应用程序上拍摄的照片,但是当我尝试访问内存以放置数据时,会出现错误
无法解码流java.io.fileNotFoundException / storage / emulated / 0 open Failed:ENOENT(没有这样的文件或目录)
这是我的代码.
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public voID onPictureTaken(byte[] data, Camera camera) { // Todo auto-generated method stub if (data != null){ //Intent mIntent = new Intent(); //mIntent.putExtra("image",imageData); mCamera.stopPrevIEw(); mPrevIEwRunning = false; mCamera.release(); try{ BitmapFactory.Options opts = new BitmapFactory.Options(); Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts); bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false); int wIDth = bitmap.getWIDth(); int height = bitmap.getHeight(); int newWIDth = 300; int newHeight = 300; // calculate the scale - in this case = 0.4f float scaleWIDth = ((float) newWIDth) / wIDth; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postscale(scaleWIDth, scaleHeight); // rotate the Bitmap matrix.postRotate(-90); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, wIDth, height, matrix, true); Camera_local_db.image.setimageBitmap(resizedBitmap); }catch(Exception e){ e.printstacktrace(); } // StoreByteImage(mContext, imageData, 50,"Imagename"); //setResult(FOTO_MODE, mIntent); setResult(585); finish(); } } }; Camera.PictureCallback jpegCallback = new Camera. PictureCallback() { @OverrIDe public voID onPictureTaken(byte[] data, Camera camera) { file dir_image2 = new file(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"dddd.jpg"); dir_image2.mkdirs(); //AGAIN CHOOSING FolDER FOR THE PICTURE(WHICH IS liKE A SURFACEVIEW //SCREENSHOT) if (!dir_image2.mkdirs()) { Log.e(TAG, "Directory not created"); } file tmpfile = new file(dir_image2,"TempGhost.jpg"); //MAKING A file IN THE PATH //dir_image2(SEE RIGHT ABOVE) AND NAMING IT "TempGhost.jpg" OR ANYTHING ELSE try {//SAVING fileOutputStream fos = new fileOutputStream(tmpfile); fos.write(data); fos.close(); //grabImage(); } catch (fileNotFoundException e) { Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); } //String path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES); file file = new file(path, "/" + dir_image2); //String path = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ // file.separator+"TempGhost.jpg");//<--- BitmapFactory.Options options = new BitmapFactory.Options();//<--- options.inPreferredConfig = Bitmap.Config.ARGB_8888;//<--- bmp1 = BitmapFactory.decodefile(tmpfile.toString(), options);//<--- //THE lines ABOVE READ THE file WE SAVED BEFORE AND CONVERT IT INTO A BitMap Camera_local_db.image.setimageBitmap(bmp1); //camera_image.setimageBitmap(bmp1); //SETTING THE BitMap AS IMAGE IN AN IMAGEVIEW(SOMETHING //liKE A BACKGROUNG FOR THE LAYOUT) // TakeScreenshot();//CALliNG THIS METHOD TO TAKE A SCREENSHOT } };解决方法:
您需要写入外部存储,确保添加了权限:
<manifest ...> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> ...</manifest>检查外部存储器是否可用于读写:
public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false;}使用公共目录的根目录而不是使用AndroID的根目录.
如果要将公共文件保存在外部存储上,请使用getExternalStoragePublicDirectory()
public file getAlbumStorageDir(String albumname) { // Get the directory for the user's public pictures directory. file file = new file(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DCIM), albumname); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } return file;}如果要保存应用程序专用的文件,请使用getExternalfilesDir()
public file getAlbumStorageDir(Context context, String albumname) { // Get the directory for the app's private pictures directory. file file = new file(context.getExternalfilesDir( Environment.DIRECTORY_DCIM), albumname); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } return file;}有关链接http://developer.android.com/training/basics/data-storage/files.html的更多信息
总结以上是内存溢出为你收集整理的android – 无法解码流java.io.FileNotFoundException / storage / emulated / 0 open failed:ENOENT(没有这样的文件或目录全部内容,希望文章能够帮你解决android – 无法解码流java.io.FileNotFoundException / storage / emulated / 0 open failed:ENOENT(没有这样的文件或目录所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)