好日子,我正试着在网格视图中显示捕捉到的图像,并保存在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)
,它指向代码中的这一行
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);
}任何帮助都将感谢
发布于 2016-05-04 10:29:54
因为BitmapFactory.decodeFile(pathName, opts)为您返回null。关于返回值的文档:
解码后的位图,如果无法解码图像数据,则为null;如果opts为非空,则返回大小(以opts.outWidth和opts.outHeight表示)。
首先,检查mCurrentPhotoPath值,如果照片在那里并且它是正确的。
第二,尝试使用BitmapFactory.decodeFile(pathName)代替。
发布于 2016-05-04 10:23:25
下面的代码对me.If很好,您需要更多详细信息http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html
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();
}
});
}
});https://stackoverflow.com/questions/37024711
复制相似问题