FileNotFoundException未连接传输端点

时间:2017-02-24 06:59:35

标签: java android io

在捕获屏幕并在关闭手机电源后将屏幕截图保存到SDCard时会发生FileNotFoundException,似乎在关机期间将缓冲区刷入磁盘的服务器已经断开,但是有人可以帮我解释更多细节吗?输出错误消息是:

  

02-24 14:03:28.180 27412 27412 D TakeScreenshotService:   @isFloatingBallVisible(),visible = false

     

02-24 14:03:29.402 27412 10176 E SaveImageInBackgroundTask:错误   SaveImageInBackgroundData,   例外:java.io的 FileNotFoundException异常:   /storage/emulated/0/Pictures/Screenshots/Screenshot_20170224-140329.jpg:   打开失败:ENOTCONN (传输端点未连接)

     

02-24 14:03:29.526 27412 27412 D TakeScreenshotService:onUnbind,   isMultiScrenshot:false intent:Intent {   cmp = com.android.systemui / .screenshot.TakeScreenshotService}

相应的代码是:

@Override
protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) {
    Log.d(TAG, "doInBackground:");
    if (params.length != 1) return null;
    if (isCancelled()) {
        params[0].clearImage();
        params[0].clearContext();
        return null;
    }

    // By default, AsyncTask sets the worker thread to have background thread priority, so bump
    // it back up so that we save a little quicker.
    Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);

    Context context = params[0].context;
    Bitmap image = params[0].image;
    Resources r = context.getResources();

    try {
        // Create screenshot directory if it doesn't exist
        mScreenshotDir.mkdirs();

        // media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds
        // for DATE_TAKEN
        long dateSeconds = mImageTime / 1000;

        // Save
        boolean compressRet = true;
        OutputStream out = new FileOutputStream(mImageFilePath);
        if(PhoneStatusBar.LEUI_ENABLE) {
            compressRet = image.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } else {
            compressRet = image.compress(Bitmap.CompressFormat.PNG, 100, out);
        }
        out.flush();
        out.close();
        if(!compressRet){
            //When storage is full screenshot image will compress failed, so we delete the file
            File f = new File(mImageFilePath);
            if(f.exists()){
                f.delete();
                Log.d(TAG,"screenshot " + mImageFilePath + " compress failed, so we delete it");
            }
            params[0].clearImage();
            params[0].result = 1;
        }else {
            // Save the screenshot to the MediaStore
            ContentValues values = new ContentValues();
            ContentResolver resolver = context.getContentResolver();
            values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
            values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
            values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds);
            values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds);
            if(PhoneStatusBar.LEUI_ENABLE) {
                values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/jpeg");
            } else {
                values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
            }
            values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth);
            values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight);
            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
            Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            // Create a share intent
            String subjectDate = DateFormat.getDateTimeInstance().format(new Date(mImageTime));
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            if(PhoneStatusBar.LEUI_ENABLE) {
                sharingIntent.setType("image/jpeg");
            } else {
                sharingIntent.setType("image/png");
            }
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mImageFileName);

            // Create a share action for the notification
            final PendingIntent callback = PendingIntent.getBroadcast(context, 0,
                    new Intent(context, GlobalScreenshot.TargetChosenReceiver.class)
                            .putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId),
                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
            Intent chooserIntent = Intent.createChooser(sharingIntent, null,
                    callback.getIntentSender());
            chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
            mNotificationBuilder.addAction(R.drawable.ic_screenshot_share,
                    r.getString(com.android.internal.R.string.share),
                    PendingIntent.getActivity(context, 0, chooserIntent,
                            PendingIntent.FLAG_CANCEL_CURRENT));

            // Create a delete action for the notification
            final PendingIntent deleteAction = PendingIntent.getBroadcast(context,  0,
                    new Intent(context, GlobalScreenshot.DeleteScreenshotReceiver.class)
                            .putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId)
                            .putExtra(GlobalScreenshot.SCREENSHOT_URI_ID, uri.toString()),
                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
            mNotificationBuilder.addAction(R.drawable.ic_screenshot_delete,
                    r.getString(com.android.internal.R.string.delete), deleteAction);

            params[0].imageUri = uri;
            params[0].image = null;
            params[0].result = 0;
        }
    } catch (Exception e) {
        // IOException/UnsupportedOperationException may be thrown if external storage is not
        // mounted
        Log.e(TAG, "error in SaveImageInBackgroundData, exception:" + e);
        params[0].clearImage();
        params[0].result = 1;
    }

    // Recycle the bitmap data
    if (image != null) {
        image.recycle();
    }

    return params[0];
}

0 个答案:

没有答案