显示在ImageView中拍摄的最后一张照片无效

时间:2015-09-13 22:20:40

标签: android android-camera

我一直试图在我的活动中加入一个ImageView来显示用相机拍摄的最后一张照片。 ImageView基本上是右下角的缩影,稍后将作为应用程序库的onclick意图..我希望它显示最后拍摄的照片以及当您打开应用程序时,其目录中的最新照片。这是以下的java代码部分:

private PictureCallback getPictureCallback() {
    PictureCallback picture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();

            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                //
                final ImageView GalleryShortcut = (ImageView) findViewById(R.id.galleryshortcut);
                BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
                scalingOptions.inSampleSize = camera.getParameters().getPictureSize().width / GalleryShortcut.getMeasuredWidth();
                final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);
                GalleryShortcut.setImageBitmap(bmp);
                GalleryShortcut.setVisibility(ImageView.VISIBLE);

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }

            Preview.refreshCamera(Camera);
        }
    };

这是.xml ImageView代码:

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:id="@+id/galleryshortcut"
        android:maxHeight="60dp"
        android:maxWidth="80dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:contentDescription="@null"/>

崩溃日志:

  

09-14 11:09:23.784 2617-2617 / cam.fornax.fornaxcam E / AndroidRuntime:FATAL EXCEPTION:main       处理:cam.fornax.fornaxcam,PID:2617       java.lang.RuntimeException:无法恢复活动{cam.fornax.fornaxcam / cam.fornax.fornaxcam.FRNXCamera}:java.lang.RuntimeException:无法连接到摄像头服务               在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)               在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)               在android.app.ActivityThread.-wrap11(ActivityThread.java)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:148)               在android.app.ActivityThread.main(ActivityThread.java:5417)               at java.lang.reflect.Method.invoke(Native Method)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)        引起:java.lang.RuntimeException:无法连接到摄像头服务               在android.hardware.Camera。(Camera.java:495)               在android.hardware.Camera.open(Camera.java:341)               at cam.fornax.fornaxcam.FRNXCamera.onResume(FRNXCamera.java:209)               在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)               在android.app.Activity.performResume(Activity.java:6312)               在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)   在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)   在android.app.ActivityThread.-wrap11(ActivityThread.java)

2 个答案:

答案 0 :(得分:0)

你必须在UI线程中这样做

GalleryShortcut.post(new Runnable() {
        @Override
        public void run() {
            GalleryShortcut.setImageBitmap(bmp);
            GalleryShortcut.setVisibility(ImageView.VISIBLE);
        }
    })

答案 1 :(得分:-1)

你能试试吗:

致电GalleryShortcut.invalidate()

后,

GalleryShortcut.setImageBitmap(bmp);

如果这不起作用,则必须在处理程序中运行图像设置。