Java tutorial
/******************************************************************************* * * Copyright (c) 2016 Mickael Gizthon . All rights reserved. Email:2013mzhou@gmail.com * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.geekandroid.sdk.sample.crop; import android.Manifest; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.app.NotificationCompat; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.Toast; import com.geekandroid.sdk.sample.R; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.util.Calendar; /** * Created by Oleksii Shliama (https://github.com/shliama). */ public class ResultActivity extends CropBaseActivity { private static final String TAG = "ResultActivity"; private static final int DOWNLOAD_NOTIFICATION_ID_DONE = 911; public static void startWithUri(@NonNull Context context, @NonNull Uri uri) { Intent intent = new Intent(context, ResultActivity.class); intent.setData(uri); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); ((ImageView) findViewById(R.id.image_view_preview)).setImageURI(getIntent().getData()); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(new File(getIntent().getData().getPath()).getAbsolutePath(), options); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setTitle(getString(R.string.format_crop_result_d_d, options.outWidth, options.outHeight)); } } @Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.menu_result, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_download) { saveCroppedImage(); } else if (item.getItemId() == android.R.id.home) { onBackPressed(); } return super.onOptionsItemSelected(item); } /** * Callback received when a permissions request has been completed. */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUEST_STORAGE_WRITE_ACCESS_PERMISSION: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { saveCroppedImage(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } private void saveCroppedImage() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getString(R.string.permission_write_storage_rationale), REQUEST_STORAGE_WRITE_ACCESS_PERMISSION); } else { Uri imageUri = getIntent().getData(); if (imageUri != null && imageUri.getScheme().equals("file")) { try { copyFileToDownloads(getIntent().getData()); } catch (Exception e) { Toast.makeText(ResultActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); Log.e(TAG, imageUri.toString(), e); } } else { Toast.makeText(ResultActivity.this, getString(R.string.toast_unexpected_error), Toast.LENGTH_SHORT) .show(); } } } private void copyFileToDownloads(Uri croppedFileUri) throws Exception { String downloadsDirectoryPath = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); String filename = String.format("%d_%s", Calendar.getInstance().getTimeInMillis(), croppedFileUri.getLastPathSegment()); File saveFile = new File(downloadsDirectoryPath, filename); FileInputStream inStream = new FileInputStream(new File(croppedFileUri.getPath())); FileOutputStream outStream = new FileOutputStream(saveFile); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); showNotification(saveFile); } private void showNotification(@NonNull File file) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "image/*"); NotificationCompat.Builder mNotification = new NotificationCompat.Builder(this); mNotification.setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notification_image_saved_click_to_preview)) .setTicker(getString(R.string.notification_image_saved)).setSmallIcon(R.drawable.ic_done) .setOngoing(false).setContentIntent(PendingIntent.getActivity(this, 0, intent, 0)) .setAutoCancel(true); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(DOWNLOAD_NOTIFICATION_ID_DONE, mNotification.build()); } }