我见过一些不将方法声明为私有或公开的示例。就像静态一样:static Bitmap resamplePic(Context context, String imagePath)
为什么?什么时候这样做? 这是我的示例代码:
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.example.android.emojify;
import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.support.v4.content.FileProvider; import android.util.DisplayMetrics; import android.view.WindowManager; import android.widget.Toast;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale;
class BitmapUtils {
private static final String FILE_PROVIDER_AUTHORITY = "com.example.android.fileprovider";
/**
* Resamples the captured photo to fit the screen for better memory usage.
*
* @param context The application context.
* @param imagePath The path of the photo to be resampled.
* @return The resampled bitmap
*/
static Bitmap resamplePic(Context context, String imagePath) {
// Get device screen size information
DisplayMetrics metrics = new DisplayMetrics();
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(metrics);
int targetH = metrics.heightPixels;
int targetW = metrics.widthPixels;
// Get the dimensions of the original bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
return BitmapFactory.decodeFile(imagePath);
}
/**
* Creates the temporary image file in the cache directory.
*
* @return The temporary image file.
* @throws IOException Thrown if there is an error creating the file
*/
static File createTempImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalCacheDir();
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
}
/**
* Deletes image file for a given path.
*
* @param context The application context.
* @param imagePath The path of the photo to be deleted.
*/
static boolean deleteImageFile(Context context, String imagePath) {
// Get the file
File imageFile = new File(imagePath);
// Delete the image
boolean deleted = imageFile.delete();
// If there is an error deleting the file, show a Toast
if (!deleted) {
String errorMessage = context.getString(R.string.error);
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
}
return deleted;
}
/**
* Helper method for adding the photo to the system photo gallery so it can be accessed
* from other apps.
*
* @param imagePath The path of the saved image
*/
private static void galleryAddPic(Context context, String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
/**
* Helper method for saving the image.
*
* @param context The application context.
* @param image The image to be saved.
* @return The path of the saved image.
*/
static String saveImage(Context context, Bitmap image) {
String savedImagePath = null;
// Create the new file in the external storage
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + ".jpg";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/Emojify");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
// Save the new Bitmap
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(context, savedImagePath);
// Show a Toast with the save location
String savedMessage = context.getString(R.string.saved_message, savedImagePath);
Toast.makeText(context, savedMessage, Toast.LENGTH_SHORT).show();
}
return savedImagePath;
}
/**
* Helper method for sharing an image.
*
* @param context The image context.
* @param imagePath The path of the image to be shared.
*/
static void shareImage(Context context, String imagePath) {
// Create the share intent and start the share activity
File imageFile = new File(imagePath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri photoURI = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
context.startActivity(shareIntent);
} }
答案 0 :(得分:0)
如果一个类没有修饰符(默认值,也称为 package-private),它只在自己的包中可见(包 被命名为相关类的组)