首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓: BitmapFactory发行

安卓: BitmapFactory发行
EN

Stack Overflow用户
提问于 2016-05-04 10:12:29
回答 2查看 115关注 0票数 1

好日子,我正试着在网格视图中显示捕捉到的图像,并保存在Sqlite Database..in中,logcat显示如下

E/BitmapFactory:无法解码流:.CameraFragment.previewCapturedImage(CameraFragment.java:259 W/System.err: java.lang.NullPointerException W/System.err: at java.lang.NullPointerException W/System.err: at CameraFragment.onActivityResult(CameraFragment.java:215)

,它指向代码中的这一行

代码语言:javascript
复制
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);

这里是代码

代码语言:javascript
复制
 @Override
        public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_camera, container, false);

            final DatabaseAdapter DBadapter = new DatabaseAdapter(getActivity());
            DBadapter.open();
            myLists = new ArrayList<Images>();
            myLists = DBadapter.getImagesList();
            // imageView = (ImageView) view.findViewById(R.id.imageListView);
            Button myButton = (Button) view.findViewById(R.id.camerabutton);
            myButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                   // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image
                   // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent


                }
            });
            adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
            myGridView = (GridView) view.findViewById(R.id.gridView);
            myGridView.setAdapter(adapter);

 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();
        return image;
    }
        }


        @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
              if (requestCode==CAMERA_CAPTURE_IMAGE_REQUEST_CODE&&resultCode == Activity.RESULT_OK){

                           previewCapturedImage();

                   Toast.makeText(getActivity(), "InvoiceFile saved successfully", Toast.LENGTH_LONG).show();

                }
                       else if (resultCode == getActivity().RESULT_CANCELED) {
                           // user cancelled Image capture
                           Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT)
                                   .show();}
                       else {
                           // failed to capture image

                           Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                                   .show();
                       }}
         private void previewCapturedImage() {


        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);

                    byte[] byte_arr = stream.toByteArray();
                    images.setImageBlob(byte_arr);
                    // Add Image Path To List
                    myLists.add(images);
                    // Refresh Gridview Image Thumbnails

                    adapter.notifyDataSetChanged();
                    DBadapter.insertImage(images);

                }

任何帮助都将感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-04 10:29:54

因为BitmapFactory.decodeFile(pathName, opts)为您返回null。关于返回值的文档

解码后的位图,如果无法解码图像数据,则为null;如果opts为非空,则返回大小(以opts.outWidth和opts.outHeight表示)。

首先,检查mCurrentPhotoPath值,如果照片在那里并且它是正确的。

第二,尝试使用BitmapFactory.decodeFile(pathName)代替。

票数 0
EN

Stack Overflow用户

发布于 2016-05-04 10:23:25

下面的代码对me.If很好,您需要更多详细信息http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html

代码语言:javascript
复制
 if(resultCode == Activity.RESULT_OK) {
             MediaScannerConnection.scanFile(getApplicationContext(), new String[] { imageUri.getPath() }, null,
               new MediaScannerConnection.OnScanCompletedListener() {
              @Override
              public void onScanCompleted(String path, Uri uri) {
               Log.i("Scanned = ", "Scanned " + path);
               runOnUiThread(new Runnable() {
                public void run() {
                 //UI Works
                 loadData();
                }
               });
   }
     });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37024711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档