java.io.file 报错,无法去code流java.io.FileNotFoundException /存储/模拟/ 0打开失败:ENOENT(没有这样的文件或目录...

hello i'm trying to save pictures taken on my application, but when i try to access the memory to place the data, an error comes out

unable to decode stream java.io.FileNotFoundException /storage/emulated/0 open failed:ENOENT(No such file or directory)

this is my code.

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

}

};

解决方案

You need to write to external storage, make sure you added the permission:

...

Checks if external storage is available for read and write:

public boolean isExternalStorageWritable() {

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

return true;

}

return false;

}

Use the root of the public directory instead of using the root of Android.

If you want to save public files on the external storage, use the 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;

}

If you want to save files that are private to your app, use the 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值