Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.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摄像头图像为空_Android_Camera_Android Gallery - Fatal编程技术网

自定义文件夹中的Android摄像头图像为空

自定义文件夹中的Android摄像头图像为空,android,camera,android-gallery,Android,Camera,Android Gallery,问候我正在尝试在我的应用程序中开发一个活动,用户使用相机捕获图像并将其保存到我的图库中的自定义文件夹中 这是我正在使用的代码 public class CameraFragment extends Fragment { private static final String TAG = "testcma"; private static final int REQUEST_DATE = 0; private static final int REQUEST_PHOTO

问候我正在尝试在我的应用程序中开发一个活动,用户使用相机捕获图像并将其保存到我的图库中的自定义文件夹中 这是我正在使用的代码

 public class CameraFragment extends Fragment {
    private static final String TAG = "testcma";
    private static final int REQUEST_DATE = 0;
    private static final int REQUEST_PHOTO = 1;
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private View mProgressContainer;
    OutputStream outStream = null;

    String fileName;
    private Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
        public void onShutter() {
            // Display the progress indicator
            mProgressContainer.setVisibility(View.VISIBLE);
        }
    };
    private Camera.PictureCallback mJpegCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            // Create a filename
            fileName = System.currentTimeMillis() + ".jpg";
            // Save the jpeg data to disk
            FileOutputStream os = null;
            boolean success = true;
            try {
                if (isDir()) {

                }
                /*
                 * os = getActivity().openFileOutput(fileName,
                 * Context.MODE_PRIVATE);
                 */// os.write(data);
            } catch (Exception e) {
                Log.e(TAG, "Error writing to file " + fileName, e);
                success = false;
            } finally {
                try {
                    if (os != null)
                        os.close();
                } catch (Exception e) {
                    Log.e(TAG, "Error closing file " + fileName, e);
                    success = false;
                }
            }
            if (success) {
                Intent i = new Intent();
                i.putExtra(Constants.EXTRA_PHOTO_FILENAME, fileName);
                getActivity().setResult(Activity.RESULT_OK, i);

            } else {
                getActivity().setResult(Activity.RESULT_CANCELED);
            }
            getActivity().finish();
            getActivity().finish();
        }

        private boolean isDir() {
            // TODO Auto-generated method stub

                // File file = createImageFile();

                final String appDirectoryName = "XYZ";
                final File imageRoot = new File(
                        Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                        appDirectoryName);
                boolean x = imageRoot.mkdirs();
                x = imageRoot.isDirectory();

                OutputStream fOut = null;
                File imagePath = new File(imageRoot.getAbsolutePath());
                File file = new File(imagePath, "GE_" + fileName); // File = new
                                                                    // File(fileName);

                try {
                    fOut = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {

                    fOut.flush();
                    fOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                ContentValues values = new ContentValues();
                values.put(Images.Media.TITLE, "title");
                values.put(Images.Media.DESCRIPTION, "descriptoin");
                values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
                values.put(Images.ImageColumns.BUCKET_ID, file.toString()
                        .toLowerCase(Locale.US).hashCode());
                values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file
                        .getName().toLowerCase(Locale.US));
                values.put("_data", file.getAbsolutePath());

                ContentResolver cr = getActivity().getContentResolver();
                cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                // final File image = new File(imageRoot,fileName );

            return false;

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_pin_camera, container,
                false);

        mProgressContainer = view
                .findViewById(R.id.pin_camera_progressContainer);
        mProgressContainer.setVisibility(View.INVISIBLE);
        ImageButton takePicButton = (ImageButton) view
                .findViewById(R.id.camera_button);
        takePicButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mCamera != null) {
                    mCamera.takePicture(mShutterCallback, null, mJpegCallback);
                }
            }
        });
        mSurfaceView = (SurfaceView) view
                .findViewById(R.id.pin_camera_surfaceView);
        SurfaceHolder holder = mSurfaceView.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            public void surfaceCreated(SurfaceHolder holder) {
                // Tell the camera to use this surface as its preview area
                try {
                    if (mCamera != null) {
                        mCamera.setPreviewDisplay(holder);
                    }
                } catch (IOException exception) {
                    Log.e(TAG, "Error setting up preview display", exception);
                }
            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                // We can no longer display on this surface, so stop the
                // preview.
                if (mCamera != null) {
                    mCamera.stopPreview();
                }
            }

            public void surfaceChanged(SurfaceHolder holder, int format, int w,
                    int h) {
                if (mCamera == null)
                    return;
                // The surface has changed size; update the camera preview size
                Camera.Parameters parameters = mCamera.getParameters();
                Size s = getBestSupportedSize(
                        parameters.getSupportedPreviewSizes(), w, h); // To be
                                                                        // reset
                                                                        // in
                                                                        // the
                                                                        // next
                                                                        // section
                parameters.setPreviewSize(s.width, s.height);
                s = getBestSupportedSize(parameters.getSupportedPictureSizes(),
                        w, h);
                parameters.setPictureSize(s.width, s.height);
                mCamera.setParameters(parameters);
                try {
                    mCamera.startPreview();
                } catch (Exception e) {
                    Log.e(TAG, "Could not start preview", e);
                    mCamera.release();
                    mCamera = null;
                }
            }
        });
        //
        return view;
    }

    @TargetApi(20)
    @Override
    public void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            mCamera = Camera.open(0);
        } else {
            mCamera = Camera.open();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    private Size getBestSupportedSize(List<Size> sizes, int width, int height) {
        Size bestSize = sizes.get(0);
        int largestArea = bestSize.width * bestSize.height;
        for (Size s : sizes) {
            int area = s.width * s.height;
            if (area > largestArea) {
                bestSize = s;
                largestArea = area;
            }
        }
        return bestSize;
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = System.currentTimeMillis() + "";
        fileName = "JPEG_" + timeStamp + "_";
        String storageDir = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                + "/XYZ";
        File dir = new File(storageDir);
        dir.mkdir();

        File image = new File(storageDir + "/" + fileName + ".jpg");

        // Save a file: path for use with ACTION_VIEW intents
        fileName = image.getAbsolutePath();
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, "title");
        values.put(Images.Media.DESCRIPTION, "descriptoin");
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(Images.ImageColumns.BUCKET_ID,
                image.toString().toLowerCase(Locale.US).hashCode());
        values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, image.getName()
                .toLowerCase(Locale.US));
        values.put("_data", image.getAbsolutePath());

        ContentResolver cr = getActivity().getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Log.i(TAG, "photo path = " + fileName);
        return image;
    }

}
公共类CameraFragment扩展片段{
私有静态最终字符串TAG=“testcma”;
私有静态最终整数请求_日期=0;
私有静态最终整数请求_PHOTO=1;
私人摄像机麦卡梅拉;
私人SurfaceView mSurfaceView;
私有视图mProgressContainer;
OutputStream outStream=null;
字符串文件名;
private Camera.ShutterCallback mShutterCallback=新建Camera.ShutterCallback(){
公共空间{
//显示进度指示器
mProgressContainer.setVisibility(View.VISIBLE);
}
};
private Camera.PictureCallback mJpegCallback=新建Camera.PictureCallback(){
公共void onPictureTaken(字节[]数据,摄像头){
//创建一个文件名
fileName=System.currentTimeMillis()+“.jpg”;
//将jpeg数据保存到磁盘
FileOutputStream os=null;
布尔成功=真;
试一试{
if(isDir()){
}
/*
*os=getActivity().openFileOutput(文件名,
*上下文。模式(私人);
*///写(数据);
}捕获(例外e){
Log.e(标记“写入文件时出错”+文件名,e);
成功=错误;
}最后{
试一试{
如果(os!=null)
os.close();
}捕获(例外e){
Log.e(标记“错误关闭文件”+文件名,e);
成功=错误;
}
}
如果(成功){
意图i=新意图();
i、 putExtra(Constants.EXTRA\u PHOTO\u文件名,文件名);
getActivity().setResult(Activity.RESULT_OK,i);
}否则{
getActivity().setResult(Activity.RESULT\u已取消);
}
getActivity().finish();
getActivity().finish();
}
专用布尔isDir(){
//TODO自动生成的方法存根
//File File=createImageFile();
最终字符串appDirectoryName=“XYZ”;
最终文件imageRoot=新文件(
环境
.getExternalStoragePublicDirectory(Environment.DIRECTORY\u图片),
appDirectoryName);
布尔x=imageRoot.mkdirs();
x=imageRoot.isDirectory();
OutputStream fOut=null;
File imagePath=新文件(imageRoot.getAbsolutePath());
File File=new文件(imagePath,“GE_”+文件名);//File=new
//文件(文件名);
试一试{
fOut=新文件输出流(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}
试一试{
fOut.flush();
fOut.close();
}捕获(IOE异常){
e、 printStackTrace();
}
ContentValues=新的ContentValues();
value.put(Images.Media.TITLE,“TITLE”);
value.put(Images.Media.DESCRIPTION,“descriptor in”);
value.put(Images.Media.DATE_take,System.currentTimeMillis());
value.put(Images.ImageColumns.BUCKET_ID,file.toString()
.toLowerCase(Locale.US).hashCode());
value.put(Images.ImageColumns.BUCKET\u显示\u名称、文件
.getName().toLowerCase(Locale.US));
value.put(“_data”,file.getAbsolutePath());
ContentResolver cr=getActivity().getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI,值);
//最终文件图像=新文件(imageRoot,文件名);
返回false;
}
};
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//TODO自动生成的方法存根
视图=充气机。充气(右布局。碎片\u销\u摄像头,容器,
假);
mProgressContainer=view
.findViewById(R.id.pin\U摄像头\U进度容器);
mProgressContainer.setVisibility(View.INVISIBLE);
ImageButton takePicButton=(ImageButton)视图
.findViewById(R.id.摄像头按钮);
takePicButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
if(mCamera!=null){
takePicture(mShutterCallback,null,mJpegCallback);
}
}
});
mSurfaceView=(SurfaceView)视图
.findViewById(R.id.pin\u camera\u surfaceView);
SurfaceHolder holder=mSurfaceView.getHolder();
holder.addCallback(新的SurfaceHolder.Callback(){
已创建的公共空白表面(表面持有人){
//告诉摄影机使用此曲面作为其预览区域
试一试{
if(mCamera!=null){
mCamera.setPreviewDisplay(支架);
}
}捕获(IOException异常){
Log.e(标记“设置预览显示时出错”,异常);
}
}
公共空间表面覆盖(表面覆盖物持有人){
//我们无法再在此曲面上显示,因此请停止
//预览。
如果(m
 String fileName = System.currentTimeMillis() + ".jpg";

 File directory = new File(Environment
    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "XYZ");

   // Save the jpeg data to disk
   boolean success = true;
   try {
     if (!directory.isDirectory() && !directory.mkdirs()) {
         throw new IOException("unable to make directories for path " + directory);
     }

     File file = new File(directory, "GE_" + fileName);
     FileOutputStream fOut = new FileOutputStream(file);

     fOut.write(data);
     fOut.close();
     MediaScannerConnection.scanFile(CameraActivity.this, new String[] {file.getAbsolutePath()},
                                new String[]{"image/jpg"}, null);
   } catch (IOException e) {
         e.printStackTrace();
   }