@Override protected List<Upload> doInBackground(List<Uri>... params) { if (params != null && params[0] != null && !params[0].isEmpty()) { Activity activity = mFragment.get().getActivity(); ContentResolver resolver = activity.getContentResolver(); photoUris = params[0]; List<Upload> uploads = new ArrayList<>(photoUris.size()); boolean hasReadPermission = PermissionUtils.hasPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE); for (Uri uri : photoUris) { try { // Check if the URI is a file as we will need read permissions for it if (!hasReadPermission && "file".equals(uri.getScheme())) { LogUtil.v("DecodeImageTask", "Received a File URI and don't have permissions"); needsPermission = true; return null; } File file = FileUtil.createFile(uri, resolver); if (FileUtil.isFileValid(file)) uploads.add(new Upload(file.getAbsolutePath())); } catch (Throwable ex) { LogUtil.e("DecodeImageTask", "Unable to decode image", ex); } } return uploads; } return null; }
private void startCamera() { mTempFile = FileUtil.createFile(FileUtil.EXTENSION_JPEG); if (FileUtil.isFileValid(mTempFile)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFile)); if (intent.resolveActivity(getActivity().getPackageManager()) != null) { startActivityForResult(intent, RequestCodes.TAKE_PHOTO); } else { Snackbar.make(mMultiView, R.string.cant_launch_intent, Snackbar.LENGTH_LONG).show(); } } }
private void uploadPhoto( @Nullable String title, @Nullable String description, @NonNull String filePath) { final File file = new File(filePath); if (!FileUtil.isFileValid(file)) { LogUtil.w(TAG, "File is invalid, bailing"); return; } LogUtil.v(TAG, "Received File to upload"); ApiClient client = new ApiClient(Endpoints.UPLOAD.getUrl(), ApiClient.HttpRequest.POST); MediaType type; if (file.getAbsolutePath().endsWith("png")) { type = MediaType.parse("image/png"); } else if (file.getAbsolutePath().endsWith("gif")) { type = MediaType.parse("image/gif"); } else { type = MediaType.parse("image/jpeg"); } MultipartBuilder builder = new MultipartBuilder() .type(MultipartBuilder.FORM) .addPart( Headers.of("Content-Disposition", "form-data; name=\"image\""), new ProgressRequestBody(file, type, this)); builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"type\""), RequestBody.create(null, "file")); if (!TextUtils.isEmpty(title)) { builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"title\""), RequestBody.create(null, title)); } if (!TextUtils.isEmpty(description)) { builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"description\""), RequestBody.create(null, description)); } RequestBody body = builder.build(); try { if (mNotificationId <= 0) mNotificationId = file.hashCode(); onUploadStarted(); JSONObject json = client.doWork(body); int status = json.getInt(ApiClient.KEY_STATUS); if (status == ApiClient.STATUS_OK) { handleResponse(json.getJSONObject(ApiClient.KEY_DATA)); } else { onUploadFailed(title, description, filePath, true); } } catch (IOException | JSONException e) { LogUtil.e(TAG, "Error uploading file", e); onUploadFailed(title, description, filePath, true); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RequestCodes.TAKE_PHOTO: if (resultCode == Activity.RESULT_OK) { if (!FileUtil.isFileValid(mTempFile)) { Snackbar.make(mMultiView, R.string.upload_camera_error, Snackbar.LENGTH_LONG).show(); return; } FileUtil.scanFile(Uri.fromFile(mTempFile), getActivity()); String fileLocation = mTempFile.getAbsolutePath(); Upload upload = new Upload(fileLocation); if (mAdapter == null) { mAdapter = new UploadPhotoAdapter(getActivity(), upload, this); mRecyclerView.setAdapter(mAdapter); } else { mAdapter.addItem(upload); } mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT); mTempFile = null; } break; case RequestCodes.SELECT_PHOTO: if (resultCode == Activity.RESULT_OK && data != null) { ClipData clipData = data.getClipData(); List<Uri> uris = null; // Check if we have multiple images if (clipData != null) { int size = clipData.getItemCount(); uris = new ArrayList<>(size); for (int i = 0; i < size; i++) { uris.add(clipData.getItemAt(i).getUri()); } } else if (data.getData() != null) { // If not multiple images, then only one was selected uris = new ArrayList<>(1); uris.add(data.getData()); } if (uris != null && !uris.isEmpty()) { mMultiView.setViewState(MultiStateView.VIEW_STATE_LOADING); new DecodeImagesTask(this).execute(uris); } else { Snackbar.make(mMultiView, R.string.error_generic, Snackbar.LENGTH_LONG).show(); } } break; case RequestCodes.UPLOAD_EDIT: if (resultCode == Activity.RESULT_OK && data != null) { final Upload upload; if (data.hasExtra(UploadEditActivity.KEY_UPDATED_UPLOAD)) { upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_UPLOAD); if (upload != null) { mAdapter.updateItem(upload); } } else if (data.hasExtra(UploadEditActivity.KEY_UPDATED_DELETED)) { upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_DELETED); if (upload != null) { final int itemIndex = mAdapter.indexOf(upload); if (itemIndex > -1) { mAdapter.removeItem(itemIndex); if (mListener != null) mListener.onPhotoRemoved(mAdapter.getItemCount()); if (mAdapter.isEmpty()) mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY); Snackbar.make(mMultiView, R.string.upload_photo_removed, Snackbar.LENGTH_LONG) .setAction( R.string.undo, new View.OnClickListener() { @Override public void onClick(View v) { mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT); mAdapter.addItem(upload, itemIndex); if (mListener != null) mListener.onPhotoAdded(); } }) .show(); } } } } break; } super.onActivityResult(requestCode, resultCode, data); }