Java Android:BitmapFactory问题
你好 我试图在gridview中显示捕获的图像,并将其保存在Sqlite数据库中..在logcat中显示 E/BitmapFactory:无法解码流: java.lang.NullPointerException W/System.err: java.lang.NullPointerException W/System.err:at .CameraFragment.previewCapturedImage(CameraFragment.java:259 W/System.err:at onActivityResult(CameraFragment.java:215) 我的代码中哪一行指向这一行Java Android:BitmapFactory问题,java,android,sqlite,gridview,Java,Android,Sqlite,Gridview,你好 我试图在gridview中显示捕获的图像,并将其保存在Sqlite数据库中..在logcat中显示 E/BitmapFactory:无法解码流: java.lang.NullPointerException W/System.err: java.lang.NullPointerException W/System.err:at .CameraFragment.previewCapturedImage(CameraFragment.java:259 W/System.err:at onAct
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
这是代码
@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);
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.fragment\u摄像头,容器,错误);
final DatabaseAdapter DBadapter=newdatabaseadapter(getActivity());
DBadapter.open();
MyList=新的ArrayList();
myLists=DBadapter.getImagesList();
//imageView=(imageView)view.findViewById(R.id.imageListView);
Button myButton=(Button)view.findViewById(R.id.camerabutton);
myButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
//fileUri=getOutputMediaFileUri(媒体类型图像);//创建一个文件以保存图像
//intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);//设置图像文件名
startActivityForResult(意图,摄像头捕获图像请求代码);//启动图像捕获意图
}
});
adapter=新的ImageListAdapter(getActivity(),R.layout.img_列表_视图,MyList);
myGridView=(GridView)view.findViewById(R.id.GridView);
setAdapter(适配器);
私有文件createImageFile()引发IOException{
//创建图像文件名
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
字符串imageFileName=“JPEG_389;”+时间戳+“389;”;
文件storageDir=Environment.getExternalStoragePublicDirectory(
环境.目录(图片);;
File image=File.createTempFile(
imageFileName,/*前缀*/
“.jpg”,/*后缀*/
storageDir/*目录*/
);
//保存文件:路径以用于操作\视图意图
mCurrentPhotoPath=“文件:”+image.getAbsolutePath();
返回图像;
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==CAMERA\u CAPTURE\u IMAGE\u REQUEST\u CODE&&resultCode==Activity.RESULT\u OK){
previewCapturedImage();
Toast.makeText(getActivity(),“InvoiceFile已成功保存”,Toast.LENGTH_LONG.show();
}
else if(resultCode==getActivity()。结果\u已取消){
//用户取消图像捕获
Toast.makeText(getActivity(),“用户已取消图像捕获”,Toast.LENGTH\u SHORT)
.show();}
否则{
//未能捕获图像
Toast.makeText(getActivity(),“抱歉!未能捕获图像”,Toast.LENGTH\u SHORT)
.show();
}}
私有void previewCapturedImage(){
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
ByteArrayOutputStream=新建ByteArrayOutputStream();
位图位图=BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
compress(bitmap.CompressFormat.PNG,0,流);
byte[]byte_arr=stream.toByteArray();
image.setImageBlob(字节\u arr);
//将图像路径添加到列表
添加(图像);
//刷新Gridview图像缩略图
adapter.notifyDataSetChanged();
DBadapter.insertImage(图像);
}
任何帮助都将不胜感激下面的代码对我来说很好。如果您需要更多详细信息
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
下面的代码对我来说很好。如果你需要更多的细节
因为
BitmapFactory.decodeFile(路径名,opts)
为您返回null
。From关于返回值:
解码后的位图,如果无法解码图像数据,则为null;如果opts为非null,则如果opts请求,则仅返回大小(在opts.outWidth和opts.outHeight中)
首先,检查mCurrentPhotoPath
值,如果照片在那里并且正确无误。其次,尝试使用
BitmapFactory.decodeFile(路径名)
。因为BitmapFactory.decodeFile(路径名,opts)
为您返回null
。关于返回值:
解码后的位图,如果无法解码图像数据,则为null;如果opts为非null,则如果opts请求,则仅返回大小(在opts.outWidth和opts.outHeight中)
首先,检查mCurrentPhotoPath
值,如果照片在那里并且正确无误。第二,尝试改用
BitmapFactory.decodeFile(路径名)
。检查BitmapFactory.decodeFile()是否返回null如果BitmapFactory.decodeFile()则检查返回nulli check mCurrentPhotoPath值它返回null我不知道为什么@EugeneKrivenjaI无法从这段代码中获得答案,请显示更多。从这段代码中不可能有mCurrentPhotoPath==null
。请检查更改它的其他位置。第二个注意,BitmapFactory.decodeFile()
接受文件路径,而不是URI,为什么文件
前缀