private void copyImageToCatroid(String originalImagePath) { int[] imageDimensions = ImageEditing.getImageDimensions(originalImagePath); if (imageDimensions[0] < 0 || imageDimensions[1] < 0) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); return; } File oldFile = new File(originalImagePath); try { if (originalImagePath.equals("")) { throw new IOException(); } String projectName = ProjectManager.getInstance().getCurrentProject().getName(); File imageFile = StorageHandler.getInstance().copyImage(projectName, originalImagePath, null); String imageName; int extensionDotIndex = oldFile.getName().lastIndexOf('.'); if (extensionDotIndex > 0) { imageName = oldFile.getName().substring(0, extensionDotIndex); } else { imageName = oldFile.getName(); } String imageFileName = imageFile.getName(); // if pixmap cannot be created, image would throw an Exception in stage // so has to be loaded again with other Config Pixmap pixmap = null; pixmap = Utils.getPixmapFromFile(imageFile); if (pixmap == null) { ImageEditing.overwriteImageFileWithNewBitmap(imageFile); pixmap = Utils.getPixmapFromFile(imageFile); if (pixmap == null) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); StorageHandler.getInstance().deleteFile(imageFile.getAbsolutePath()); return; } } pixmap = null; updateLookAdapter(imageName, imageFileName); } catch (IOException e) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); } getLoaderManager().destroyLoader(ID_LOADER_MEDIA_IMAGE); getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED)); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { String originalImagePath = ""; CursorLoader cursorLoader = (CursorLoader) loader; boolean catchedExpetion = false; if (data == null) { originalImagePath = cursorLoader.getUri().getPath(); } else { int columnIndex = data.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); data.moveToFirst(); try { originalImagePath = data.getString(columnIndex); } catch (CursorIndexOutOfBoundsException e) { catchedExpetion = true; } } if (catchedExpetion || (data == null && originalImagePath.equals(""))) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); return; } copyImageToCatroid(originalImagePath); }
private void loadPictureFromCameraIntoCatroid() { if (lookFromCameraUri != null) { String originalImagePath = lookFromCameraUri.getPath(); int[] imageDimensions = ImageEditing.getImageDimensions(originalImagePath); if (imageDimensions[0] < 0 || imageDimensions[1] < 0) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); return; } copyImageToCatroid(originalImagePath); File pictureOnSdCard = new File(lookFromCameraUri.getPath()); pictureOnSdCard.delete(); } }
private void copyLook(int position) { LookData lookData = lookDataList.get(position); try { String projectName = ProjectManager.getInstance().getCurrentProject().getName(); StorageHandler.getInstance().copyImage(projectName, lookData.getAbsolutePath(), null); String imageName = lookData.getLookName() + "_" + getString(R.string.copy_look_addition); String imageFileName = lookData.getLookFileName(); updateLookAdapter(imageName, imageFileName); } catch (IOException e) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); e.printStackTrace(); } getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED)); }
private void loadPocketPaintImageIntoCatroid(Intent intent) { Bundle bundle = intent.getExtras(); String pathOfPocketPaintImage = bundle.getString(Constants.EXTRA_PICTURE_PATH_POCKET_PAINT); int[] imageDimensions = ImageEditing.getImageDimensions(pathOfPocketPaintImage); if (imageDimensions[0] < 0 || imageDimensions[1] < 0) { Utils.showErrorDialog(getActivity(), this.getString(R.string.error_load_image)); return; } String actualChecksum = Utils.md5Checksum(new File(pathOfPocketPaintImage)); // If look changed --> saving new image with new checksum and changing lookData if (!selectedLookData.getChecksum().equalsIgnoreCase(actualChecksum)) { String oldFileName = selectedLookData.getLookFileName(); String newFileName = oldFileName.substring(oldFileName.indexOf('_') + 1); // HACK for https://github.com/Catrobat/Catroid/issues/81 if (!newFileName.endsWith(".png")) { newFileName = newFileName + ".png"; } String projectName = ProjectManager.getInstance().getCurrentProject().getName(); try { File newLookFile = StorageHandler.getInstance() .copyImage(projectName, pathOfPocketPaintImage, newFileName); File temporaryPictureFileInPocketPaint = new File(pathOfPocketPaintImage); temporaryPictureFileInPocketPaint.delete(); // delete temp file in paintroid StorageHandler.getInstance() .deleteFile( selectedLookData.getAbsolutePath()); // reduce usage in container or delete it selectedLookData.setLookFilename(newLookFile.getName()); selectedLookData.resetThumbnailBitmap(); } catch (IOException e) { e.printStackTrace(); } } }
private boolean handleOkButton() { EditText editText = (EditText) dialogView.findViewById(R.id.dialog_new_object_name_edit_text); String newSpriteName; if (editText.length() == 0) { newSpriteName = editText.getHint().toString(); } else { newSpriteName = editText.getText().toString().trim(); } if (newSpriteName.contains(".")) { int fileExtensionPosition = newSpriteName.indexOf("."); newSpriteName = newSpriteName.substring(0, fileExtensionPosition); } ProjectManager projectManager = ProjectManager.getInstance(); if (newSpriteName.equalsIgnoreCase("")) { Utils.showErrorDialog(getActivity(), R.string.spritename_invalid); return false; } if (projectManager.spriteExists(newSpriteName)) { Utils.showErrorDialog(getActivity(), R.string.spritename_already_exists); return false; } Sprite sprite = new Sprite(newSpriteName); projectManager.addSprite(sprite); LookData lookData; try { File newLookFile = StorageHandler.getInstance() .copyImage(projectManager.getCurrentProject().getName(), lookUri.getPath(), null); String imageFileName = newLookFile.getName(); Utils.rewriteImageFileForStage(getActivity(), newLookFile); lookData = new LookData(); lookData.setLookFilename(imageFileName); lookData.setLookName(newSpriteName); } catch (IOException exception) { Utils.showErrorDialog(getActivity(), R.string.error_load_image); exception.printStackTrace(); return false; } sprite.getLookDataList().add(lookData); if (requestedAction == ActionAfterFinished.ACTION_UPDATE_SPINNER && spinnerAdapter != null) { Intent broadcastIntent; broadcastIntent = new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED); getActivity().sendBroadcast(broadcastIntent); spinnerAdapter.refreshSpinnerAfterNewSprite(getActivity(), newSpriteName); } else { Intent broadcastIntent; broadcastIntent = new Intent(ScriptActivity.ACTION_SPRITES_LIST_CHANGED); getActivity().sendBroadcast(broadcastIntent); } if (requestedAction == ActionAfterFinished.ACTION_FORWARD_TO_NEW_OBJECT) { projectManager.setCurrentSprite(sprite); Intent intent = new Intent(getActivity(), ProgramMenuActivity.class); intent.putExtra( ProgramMenuActivity.FORWARD_TO_SCRIPT_ACTIVITY, ScriptActivity.FRAGMENT_SCRIPTS); startActivity(intent); } dismiss(); return true; }