Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从线程将bmp图像保存到SD卡_Android_File_Opencv - Fatal编程技术网

Android 从线程将bmp图像保存到SD卡

Android 从线程将bmp图像保存到SD卡,android,file,opencv,Android,File,Opencv,我正在Android上试用openCV示例。当我尝试将bmp文件保存到SD卡时,每个线程都使用以下代码: String filePath = "/sdcard"; // some times it may be only /sdcard not /mnt/sdcard filePath += "newFileName.jpg"; try { bmp.compress(Bitmap.CompressFormat.JPEG, 100, new

我正在Android上试用openCV示例。当我尝试将bmp文件保存到SD卡时,每个线程都使用以下代码:

String filePath = "/sdcard"; // some times it may be only /sdcard not /mnt/sdcard
        filePath += "newFileName.jpg";
        try {
             bmp.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
        } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i(TAG, "IOException Occurred!!!");   
        }
日志始终显示“发生IOException”,SD卡上未保存任何文件

下面是完整的类代码

public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private static final String TAG = "Sample::SurfaceView";

private SurfaceHolder       mHolder;
private VideoCapture        mCamera;
private FpsMeter            mFps;

public SampleCvViewBase(Context context) {
    super(context);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mFps = new FpsMeter();
    Log.i(TAG, "Instantiated new " + this.getClass());
}

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {//called immediately after the structural change of surface
    Log.i(TAG, "surfaceCreated");
    synchronized (this) {
        if (mCamera != null && mCamera.isOpened()) {
            Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
            List<Size> sizes = mCamera.getSupportedPreviewSizes();
            Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
            int mFrameWidth = width;
            int mFrameHeight = height;

            // selecting optimal camera preview size
            {
                double minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - height) < minDiff) {
                        mFrameWidth = (int) size.width;
                        mFrameHeight = (int) size.height;
                        minDiff = Math.abs(size.height - height);
                    }
                }
            }

            mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
            mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
    if (mCamera.isOpened()) {
        (new Thread(this)).start();     //new thread
    } else {
        mCamera.release();
        mCamera = null;
        Log.e(TAG, "Failed to open native camera");
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i(TAG, "surfaceDestroyed");
    if (mCamera != null) {
        synchronized (this) {
            mCamera.release();                      
            mCamera = null;
        }
    }
}

protected abstract Bitmap processFrame(VideoCapture capture) throws IOException;

public void run() {
    Log.i(TAG, "Starting processing thread");       //send out log notice
    mFps.init();                                //instance of Fps meter    

    while (true) {
        Bitmap bmp = null;

        synchronized (this) {
            if (mCamera == null)                    //instance of video capture
                break;

            if (!mCamera.grab()) {
                Log.e(TAG, "mCamera.grab() failed");
                break;
            }

            try {
                bmp = processFrame(mCamera);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mFps.measure();
        }

        if (bmp != null) {
            Canvas canvas = mHolder.lockCanvas();
            if (canvas != null) {
                canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
                mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
                mHolder.unlockCanvasAndPost(canvas);
            }
            String filePath = "/sdcard"; // some times it may be only /sdcard not /mnt/sdcard
            filePath += "newFileName.jpg";
            try {
                 bmp.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
            } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i(TAG, "IOExcpetion Occurred!!!");   
            }
        }
公共抽象类SampleCvViewBase扩展了SurfaceView实现了SurfaceHolder.Callback,Runnable{
私有静态最终字符串TAG=“Sample::SurfaceView”;
私人地勤人员;
私有视频捕获mCamera;
私人电表;
公共SampleCvViewBase(上下文){
超级(上下文);
mHolder=getHolder();
mHolder.addCallback(this);
mFps=新FpsMeter();
Log.i(标记“实例化新”+this.getClass());
}
public void surfaceChanged(SurfaceHolder _holder,int格式,int宽度,int高度){//在曲面结构更改后立即调用
Log.i(标签“表面处理”);
已同步(此){
if(mCamera!=null&&mCamera.isOpened()){
Log.i(标记“before mCamera.getSupportedPreviewSizes()”;
列表大小=mCamera.getSupportedPreviewSizes();
Log.i(标记“在mCamera.getSupportedPreviewSizes()之后”;
int mFrameWidth=宽度;
int mframehight=高度;
//选择最佳相机预览尺寸
{
double minDiff=double.MAX_值;
用于(尺寸:尺寸){
if(数学绝对值(尺寸高度-高度)
以下是我在DDMS中得到的信息:

03-07 08:58:11.639: W/TextLayoutCache(986): computeValuesWithHarfbuzz -- need to force to single run
03-07 08:58:11.709: W/System.err(986): java.io.FileNotFoundException: /sdcardnewFileName.jpg: open failed: EROFS (Read-only file system)
03-07 08:58:11.769: W/System.err(986):  at libcore.io.IoBridge.open(IoBridge.java:406)
03-07 08:58:11.769: W/System.err(986):  at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-07 08:58:11.809: W/System.err(986):  at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
03-07 08:58:11.829: W/System.err(986):  at org.opencv.samples.fd.SampleCvViewBase.run(SampleCvViewBase.java:125)
03-07 08:58:11.829: W/System.err(986):  at org.opencv.samples.fd.FdView.run(FdView.java:107)
03-07 08:58:11.829: W/System.err(986):  at java.lang.Thread.run(Thread.java:856)
03-07 08:58:11.829: W/System.err(986): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
03-07 08:58:11.859: W/System.err(986):  at libcore.io.Posix.open(Native Method)
03-07 08:58:11.859: W/System.err(986):  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
03-07 08:58:11.889: W/System.err(986):  at libcore.io.IoBridge.open(IoBridge.java:390)
03-07 08:58:11.889: W/System.err(986):  ... 5 more

03-07 08:58:11.919: I/Sample::SurfaceView(986): IOExcpetion Occurred!!!

03-07 08:58:12.489: D/dalvikvm(986): GC_FOR_ALLOC freed 1209K, 14% free 9902K/11399K, paused 29ms
03-07 08:58:12.489: I/dalvikvm-heap(986): Grow heap (frag case) to 10.925MB for 1228816-byte allocation
03-07 08:58:12.619: D/dalvikvm(986): GC_CONCURRENT freed <1K, 3% free 11102K/11399K, paused 3ms+45ms
03-07 08:58:11.639:W/TextLayoutCache(986):computeValuesWithHarfbuzz--需要强制运行一次
03-07 08:58:11.709:W/System.err(986):java.io.FileNotFoundException:/sdcardnewFileName.jpg:open失败:EROFS(只读文件系统)
03-07 08:58:11.769:W/System.err(986):位于libcore.io.IoBridge.open(IoBridge.java:406)
03-07 08:58:11.769:W/System.err(986):位于java.io.FileOutputStream。(FileOutputStream.java:88)
03-07 08:58:11.809:W/System.err(986):位于java.io.FileOutputStream。(FileOutputStream.java:73)
03-07 08:58:11.829:W/System.err(986):位于org.opencv.samples.fd.SampleCvViewBase.run(SampleCvViewBase.java:125)
03-07 08:58:11.829:W/System.err(986):位于org.opencv.samples.fd.FdView.run(FdView.java:107)
03-07 08:58:11.829:W/System.err(986):位于java.lang.Thread.run(Thread.java:856)
03-07 08:58:11.829:W/System.err(986):原因:libcore.io.ErrnoException:打开失败:EROFS(只读文件系统)
03-07 08:58:11.859:W/System.err(986):位于libcore.io.Posix.open(本机方法)
03-07 08:58:11.859:W/System.err(986):位于libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
03-07 08:58:11.889:W/System.err(986):位于libcore.io.IoBridge.open(IoBridge.java:390)
03-07 08:58:11.889:W/系统错误(986):…还有5个
03-07 08:58:11.919:I/样本::SurfaceView(986):发生IOException!!!
03-07 08:58:12.489:D/dalvikvm(986):释放1209K的所有元素的GC_,14%的自由元素9902K/11399K,暂停29毫秒
03-07 08:58:12.489:I/dalvikvm堆(986):为1228816字节分配将堆(frag大小写)增长到10.925MB

03-07 08:58:12.619:D/dalvikvm(986):GC_CONCURRENT freed您试图写入文件系统根目录,因为您的文件路径中缺少正斜杠:您使用
/sdcardnewFileName.jpg
而不是
/sdcard/newFileNa
File root = Environment.getExternalStorageDirectory();
File imgFile = new File(root, "newFileName.jpg");
FileOutputStream fos = new FileOutputStream(imgFile);