打印后返回启动画面

时间:2017-05-13 23:12:23

标签: java android android-intent cycle

我在Android工作室内编写应用程序。现在我希望我的应用程序永远不会关闭并作为一个循环工作。我知道我可以使用Intent类来完成这项工作,并为我的应用程序中的所有活动配置Manifest.xml文件。

但要打印,我会在我的应用程序之外进行打印设置的打印预览。此时 - 打印完成时 - 预览将关闭并自动返回到我上次的活动(打印预览之前的活动)。现在,我想回到起始页(category.LAUNCHER)而不是前一个活动。

但这与使用Intent类进行编程无关,因为printpreview没有xml文件。因为它是一个创建位图文件并在我的应用程序之外进行打印的方法。

val s = null;
val s2 = "abc" + s;     // abcnull
val s3 = "abc" + s!!;   // NPE

如果我创建一个Intent对象,它将跳过printpreview部分或在打印后崩溃。所以我想知道是否有另一种可能的方法来解决这个问题并保持应用程序运行?

 /**
 * Method which creates a bitmap of the picture with the Odisee logo and 
 * goes to the printpreviewpage with settings.
 *
 */
private void doPhotoPrint() {
    try {
        //Creates object from PrintHelper class
        PrintHelper photoPrinter = new PrintHelper(this);

        //Scales image
        photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FILL);

        // Find the last picture
        String[] projection = new String[]{
                MediaStore.Images.ImageColumns._ID,
                MediaStore.Images.ImageColumns.DATA,
                MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                MediaStore.Images.ImageColumns.DATE_TAKEN,
                MediaStore.Images.ImageColumns.MIME_TYPE};
        final Cursor cursor = getApplicationContext().getContentResolver()
                .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                        null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

        String imageLocation = null;

        if (cursor.moveToFirst()) {
            imageLocation = cursor.getString(1);

        }
        File imageFile = new File(imageLocation);

        //Creates bitmapfile to print
        if (imageFile.exists()) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            //Gets the resources (last taken picture + Odisee logo)
            Bitmap bitmap = BitmapFactory.decodeFile(imageLocation);
            Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.logo_odisee2);
            Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

            //Draws the Odisee logo onto the picture
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bitmap, new Matrix(),null);
            canvas.drawBitmap(bitmap2,950,550,null);

            //Sets printer orientation to landscape
            photoPrinter.setOrientation(PrintHelper.ORIENTATION_LANDSCAPE);

            //Goes to screen to print the bitmap
            photoPrinter.printBitmap("photobooth", bmOverlay);
        }
    }catch (Exception e) {
        System.err.println("Fout!");
    }
}

0 个答案:

没有答案
相关问题