在 DCIM/pictures 中通过相机意图保存图片的自定义名称
Save Custom name for picture via camera intent in DCIM/pictures
我已经关注并从其他答案中实际提取了代码,但它仍然无法正常工作。
点击图片后默认保存在DCIM/camera中,名称如20150910_111841.jpg
(date_timeInMilliseconds
),但我想保存为"myName_date.jpg"。
public void OnClick(){
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+System.currentTimeMillis()+"jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
switch(requestCode) {
case REQUEST_IMAGE_CAPTURE:
Bitmap bm = BitmapFactory.decodeFile(mMediaUri.getPath());
//imgVIew.setImageBitmap(bm);
Bitmap scaledBitmap=ScaleImage(bm);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG,100, bos);
//Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bos.toByteArray()));
imgVIew.setImageBitmap(scaledBitmap);
imageView.setImage(ImageSource.bitmap(scaledBitmap));
imageView1.setImage(ImageSource.bitmap(bm));
break;
case REQUEST_GALLERY:
//Uri selectedImage = imageReturnedIntent.getData();
//imageview.setImageURI(selectedImage);
break;
}//Close switch statement
}else {
Log.d("Error ", "user canceled !");
}
}
我还没有为图库部分添加代码,但这应该不是问题。截至目前,无论我做什么,它都被保存为默认值,但我意识到如果人们没有 SD 卡,那么它可能会导致问题。我宁愿在默认 DCIM 位置提供此图片,但使用自定义名称。谢谢!
会喜欢,这个
File fPath = getAlbumStorageDir("myName_date");
File f = new File(fPath, "myName_date" + Calendar.getInstance().getTimeInMillis() + ".jpg");
不是设备问题,
例如我的工作代码,我有这样的成就,
public static Uri getUri(String filePath) {
File media = new File(filePath);
Uri uri = Uri.fromFile(media);
return uri;
}
public static File getAlbumStorageDir(String albumName) {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (file.exists()) {
Log.d(LOGTAG, "storeage file exists");
}
if (!(file.mkdirs() || file.isDirectory())) {
Log.e(LOGTAG, "Directory not created");
}
return file;
}
public static class SaveImageTask extends AsyncTask<Bitmap, Void, String> {
private SaveImageListener listener;
public SaveImageTask(SaveImageListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap bitmapToSave = params[0];
File fPath = getAlbumStorageDir("AppName");
File f = new File(fPath, "AppName" + Calendar.getInstance().getTimeInMillis() + ".jpg");
String filePath = null;
try {
FileOutputStream strm = new FileOutputStream(f);
bitmapToSave.compress(Bitmap.CompressFormat.JPEG, 100, strm);
strm.close();
filePath = f.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
@Override
protected void onPostExecute(String filePath) {
super.onPostExecute(filePath);
listener.onImageSaved(filePath);
}
interface SaveImageListener {
void onImageSaved(String filePath);
}
}
要使用自定义名称,请替换此 -
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+System.currentTimeMillis()+"jpg"));
有了这个-
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+"Your Custom Name"+System.currentTimeMillis()+"jpg"));
尝试使用以下代码以您想要的名称保存图像:
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
/*
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
*/
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "the_name_of_directory");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("canberra trailpass", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"myName"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
Uri mImageCaptureUri;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "example.jpg"));
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(takePictureIntent, 1);
onActivityResult :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
if(mImageCaptureUri!=null) {
imageName = "";
Bitmap bitmap = getCameraImage(activity, mImageCaptureUri);
imgView.setImageBitmap(bitmap );
removeFile(mImageCaptureUri.getPath());
break;
}
}
}
获取相机图像:
public static Bitmap getCameraImage(Activity activity, Uri mImageCaptureUri){
if(mImageCaptureUri==null) {
return null;
} else {
activity.getContentResolver().notifyChange(mImageCaptureUri, null);
ContentResolver cr = activity.getContentResolver();
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
AssetFileDescriptor fileDescriptor =null;
fileDescriptor =
cr.openAssetFileDescriptor( mImageCaptureUri, "r");
bitmap
= BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(), null, options);
//bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageCaptureUri);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
我已经关注并从其他答案中实际提取了代码,但它仍然无法正常工作。
点击图片后默认保存在DCIM/camera中,名称如20150910_111841.jpg
(date_timeInMilliseconds
),但我想保存为"myName_date.jpg"。
public void OnClick(){
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+System.currentTimeMillis()+"jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
switch(requestCode) {
case REQUEST_IMAGE_CAPTURE:
Bitmap bm = BitmapFactory.decodeFile(mMediaUri.getPath());
//imgVIew.setImageBitmap(bm);
Bitmap scaledBitmap=ScaleImage(bm);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG,100, bos);
//Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bos.toByteArray()));
imgVIew.setImageBitmap(scaledBitmap);
imageView.setImage(ImageSource.bitmap(scaledBitmap));
imageView1.setImage(ImageSource.bitmap(bm));
break;
case REQUEST_GALLERY:
//Uri selectedImage = imageReturnedIntent.getData();
//imageview.setImageURI(selectedImage);
break;
}//Close switch statement
}else {
Log.d("Error ", "user canceled !");
}
}
我还没有为图库部分添加代码,但这应该不是问题。截至目前,无论我做什么,它都被保存为默认值,但我意识到如果人们没有 SD 卡,那么它可能会导致问题。我宁愿在默认 DCIM 位置提供此图片,但使用自定义名称。谢谢!
会喜欢,这个
File fPath = getAlbumStorageDir("myName_date");
File f = new File(fPath, "myName_date" + Calendar.getInstance().getTimeInMillis() + ".jpg");
不是设备问题, 例如我的工作代码,我有这样的成就,
public static Uri getUri(String filePath) {
File media = new File(filePath);
Uri uri = Uri.fromFile(media);
return uri;
}
public static File getAlbumStorageDir(String albumName) {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (file.exists()) {
Log.d(LOGTAG, "storeage file exists");
}
if (!(file.mkdirs() || file.isDirectory())) {
Log.e(LOGTAG, "Directory not created");
}
return file;
}
public static class SaveImageTask extends AsyncTask<Bitmap, Void, String> {
private SaveImageListener listener;
public SaveImageTask(SaveImageListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap bitmapToSave = params[0];
File fPath = getAlbumStorageDir("AppName");
File f = new File(fPath, "AppName" + Calendar.getInstance().getTimeInMillis() + ".jpg");
String filePath = null;
try {
FileOutputStream strm = new FileOutputStream(f);
bitmapToSave.compress(Bitmap.CompressFormat.JPEG, 100, strm);
strm.close();
filePath = f.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
@Override
protected void onPostExecute(String filePath) {
super.onPostExecute(filePath);
listener.onImageSaved(filePath);
}
interface SaveImageListener {
void onImageSaved(String filePath);
}
}
要使用自定义名称,请替换此 -
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+System.currentTimeMillis()+"jpg"));
有了这个-
mMediaUri=Uri.fromFile(new File(f, "MEDIT"+"Your Custom Name"+System.currentTimeMillis()+"jpg"));
尝试使用以下代码以您想要的名称保存图像:
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
/*
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
*/
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "the_name_of_directory");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("canberra trailpass", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"myName"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
Uri mImageCaptureUri;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "example.jpg"));
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(takePictureIntent, 1);
onActivityResult :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
if(mImageCaptureUri!=null) {
imageName = "";
Bitmap bitmap = getCameraImage(activity, mImageCaptureUri);
imgView.setImageBitmap(bitmap );
removeFile(mImageCaptureUri.getPath());
break;
}
}
}
获取相机图像:
public static Bitmap getCameraImage(Activity activity, Uri mImageCaptureUri){
if(mImageCaptureUri==null) {
return null;
} else {
activity.getContentResolver().notifyChange(mImageCaptureUri, null);
ContentResolver cr = activity.getContentResolver();
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
AssetFileDescriptor fileDescriptor =null;
fileDescriptor =
cr.openAssetFileDescriptor( mImageCaptureUri, "r");
bitmap
= BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(), null, options);
//bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageCaptureUri);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}