英文:
save image file with increment at the end of file name
问题
private File saveBitMap(Context context, View drawView) {
File pictureFileDir = new File(Environment.getExternalStorageDirectory() + "/" + "Frames");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if (!isDirectoryCreated) {
Log.i("ATG", "Can't create directory to save the image");
}
return null;
}
String filename = pictureFileDir.getPath() + File.separator + "Frame" + System.currentTimeMillis() + ".jpg";
File pictureFile = new File(filename);
Bitmap bitmap = getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery(context, pictureFile.getAbsolutePath());
return pictureFile;
}
英文:
please someone help me "To save an image file with increment at end of file name" like("image 1.jpg , image 2.jpg , etc..")
here is my code
please some help me to make this,i am new learner to android-studio.
private File saveBitMap(Context context, View drawView) {
File pictureFileDir = new File(Environment.getExternalStorageDirectory()+"/"+"Frames");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if(!isDirectoryCreated) {
Log.i("ATG", "Can't create directory to save the image");
}
return null;
}
String filename = pictureFileDir.getPath() +File.separator+"Frame"+ System.currentTimeMillis()+".jpg";
File pictureFile = new File(filename);
Bitmap bitmap =getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery( context,pictureFile.getAbsolutePath());
return pictureFile;
}
答案1
得分: 0
如果我正确理解了您的问题,那么我假设您希望将 pictureFile
名称附加一个整数(自动递增方式)。
您可以通过维护一个全局变量 int imageCount = 1
来实现这一点。
然后在创建 fileName
时进行附加:
int imageCount = 1;
private File saveBitMap(Context context, View drawView) {
File pictureFileDir = new File(Environment.getExternalStorageDirectory() + "/" + "Frames");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if (!isDirectoryCreated) {
Log.i("ATG", "Can't create directory to save the image");
}
return null;
}
String filename = pictureFileDir.getPath() + File.separator + "Frame" + System.currentTimeMillis() + "" + (imageCount++) + ".jpg";
File pictureFile = new File(filename);
Bitmap bitmap = getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery(context, pictureFile.getAbsolutePath());
return pictureFile;
}
英文:
If I have understood your question properly then, I assume you want your pictureFile
name to be appended by an integer (in auto-incrementing fashion).
You could do that by maintaining a global variable as int imageCount = 1
and then appending it while creating fileName
int imageCount = 1;
private File saveBitMap(Context context, View drawView) {
File pictureFileDir = new File(Environment.getExternalStorageDirectory()+"/"+"Frames");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if(!isDirectoryCreated) {
Log.i("ATG", "Can't create directory to save the image");
}
return null;
}
String filename = pictureFileDir.getPath() +File.separator+"Frame"+ System.currentTimeMillis()+""+(imageCount++)+".jpg";
File pictureFile = new File(filename);
Bitmap bitmap =getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery( context,pictureFile.getAbsolutePath());
return pictureFile;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论