using camera2 api i m save file like this
if (Build.VERSION.SDK_INT <=Build.VERSION_CODES.P){
final String folderPath = Environment.getExternalStorageDirectory() + "/Pikpap/";
file1 = new File(folderPath);
} else{
file1 = new File( CameraFragment.view.getContext().getExternalFilesDir()+ "/Pikpap/");
}
if (!file1.exists()) {
file1.mkdirs();
}
String fullName = file1.getAbsolutePath() + "Pikpap" + currentTime+name + ".jpg";
File file = new File(fullName);
but some time file not save and image blank set on image view
this issue created on all devices
Related
I have downloaded the image file and saved in a directory but it returning empty list files . Files are downloaded and stored in directory. I tried many solutions still it not solved. I am working on oreo devices and checked the runtime permission.
The below line is for checking the files inside the directory
try{
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
if (!giftFolder.exists()) {
giftFolder.mkdir();
}
File directory = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
if(directory.isDirectory()){
Log.d("Files", "directory: "+ directory.isDirectory());
}
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
}catch (Exception e){
e.printStackTrace();
}
The below code is for downloading file using download manger
public static void downloadFile(Context context, String url, String fileName) {
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(url));
downloadRequest.setTitle(fileName);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
downloadRequest.allowScanningByMediaScanner();
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
File giftFolder = new File(context.getFilesDir() + "/SamplePhoto");
if (!giftFolder.exists()) {
giftFolder.mkdir();
}
// downloadRequest.setDestinationInExternalPublicDir(Environment.getExternalStorageState()+"/SamplePhoto/", fileName);
// downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
downloadRequest.setDestinationInExternalPublicDir(context.getFilesDir() + "/SamplePhoto/", fileName);
Log.e("DownloadingPath",""+ context.getFilesDir() + "/SamplePhoto/"+ fileName);
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(downloadRequest);
}
Try changing:
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
To
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto");
And anywhere else you use "/SamplePhoto/" too. You dont need the tailing slash when accessing a directory.
I've tried numerous forms but I'm not getting the size of my photos at all, they're coming out with more than 3mb and I need them to upload numerous pictures via FTP. Follow code for someone could demonstrate a functional gradient
File createImageFile() throws IOException {
Logger.getAnonymousLogger().info("Generating the image - method started");
// New Image
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
String imageFileName = os_id+ "_"+ txtCheckList_id.getText().toString() + "_" + txtCheckListItens_id.getText().toString() + "_1A_" + timeStamp;
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "GRP");
Logger.getAnonymousLogger().info("Storage directory set");
if (!storageDirectory.exists()) storageDirectory.mkdir();
File image = new File(storageDirectory, imageFileName + ".png");
Logger.getAnonymousLogger().info("File name and path set");
mImageFileLocation = image.getAbsolutePath();
return image;
}
You can simply use file.length() to get the size of the file, please check the mentioned example,
int file_size = Integer.parseInt(String.valueOf(file.length()/1024));
I wrote a small Android App to record audio and save this on the disk of the smartphone. Now I've got the problem that it's saved on the wrong location with the wrong name and I can't see why. (I'm relative new to Android programming)
public void startRecording(View view) {
File folder = new File(Environment.getExternalStorageDirectory() + "/" + R.string.app_name);
file_name = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + R.string.app_name;
if(!folder.exists()) {
boolean created = folder.mkdirs();
if(!created) { Log.i(LOG_TAG, "Could not create folder/s."); return; }
}
if(editText.getText() != null)
file_name += editText.getText() + ".3gp";
else
file_name += Calendar.getInstance().getTime() + ".3gp";
outputFile = new File(file_name);
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(file_name);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
Toast.makeText(CaptureActivity.this, R.string.toast_recording_start, Toast.LENGTH_SHORT).show();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
Toast.makeText(CaptureActivity.this, R.string.toast_recording_failed, Toast.LENGTH_LONG).show();
mediaRecorder = null;
}
}
The file should be saved in a folder called TrueCapture and the file itself should be called ggj.3gp .
But the file is saved on the internal storage under the name 2131099680ggj.3gp .
The next problem there is, I can only find the file with the explorer app from my smartphone. The PC can't find the file and no other app.
Some Details:
Android 6
File Name is wrong (2131099680ggj.3gp instead of ggj.3gp)
File is saved in internal storage in no specific folder instead of the external storage (SD card is in the smartphone) in a new folder called "TrueCapture"
No other app seems to know about this file except for the explorer app of the smartphone
R.string.app_name is the ID assigned to your resource in the R class, an int.
To get your String resource, you might wanna use the getString() method of Context.
Something like this should work:
String ext = ".3gp";
File folder = new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.app_name));
if(!folder.exists()) {
folder.mkdirs();
}
file_name = folder.getAbsolutePath() + "/";
if(editText.getText().length() > 0) {
file_name += editText.getText().toString() + ext;
} else {
file_name += Calendar.getInstance().getTimeInMillis() + ext;
}
File outputFile = new File(file_name);
// ...
You also need to add the WRITE_EXTERNAL_STORAGE permission to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I want to save multiple images in a folder on sdcard but when saving new image, previous ones gets overwritten. How can I save multiple images in my folder?
File mydirectory = new File(Environment.getExternalStorageDirectory() + "/AppFolder");
if(!mydirectory.exists()){
mydirectory.mkdir();
}
if(mydirectory.exists()){
try {
File root = new File(Environment.getExternalStorageDirectory() +"/AppFolder");
File sdImageMainDirectory = new File(root , "pic.png");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
finish();
}
}
}
private void startCameraActivity() {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 101);
}
You are saving each file with name as pic.png.
You need to change the file name each time.
Edit:
In addition to answer by #David Xu, if you want to have unique name each time, you can just append a unix timestamp at the end of the file name as follows
dImageMainDirectory = new File(root, "pic_" + (System.currentTimeMillis() / 1000L) + ".png");
change
File sdImageMainDirectory = new File(root , "pic.png");
to
File sdImageMainDirectory = null;
int i = 0;
do {
sdImageMainDirectory = new File(root, "pic-" + i + ".png");
i++;
} while (sdImageMainDirectory.exists());
Every file you create has the same name. Here's a quick and dirty solution:
try{
File root = new File(Environment.getExternalStorageDirectory() +"/AppFolder");
int i = 0;
File sdImageMainDirectory = new File(root , "pic.png");
while(sdImageMainDirectory.exists()){
i++
sdImageMainDirectory = new File(root , "pic" +i+ ".png");
}
outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
You are using the same name for every picture which is why it is getting overwritten
File sdImageMainDirectory = new File(root , "pic.png");
you are naming them all pic.png
Try developing a naming convention like pic1.png, pic2.png or you can save the png as the name of the current timestamp like this:
//get timestamp into string
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();
String timeStamp = df.format(today);
File sdImageMainDirectory = new File(root , timeStamp + ".png");
How do i save a .mp3 file to the Music directory on the sdCard.
The code below always saves the file to the downloads folder without any extensions
private FileOutputStream getOutStream(String fileName) throws FileNotFoundException{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String sdpath = Environment.getExternalStorageDirectory()
+ "/";
mSavePath = sdpath + "download";
File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
File saveFile = new File(mSavePath, fileName);
return new FileOutputStream(saveFile);
}else{
mSavePath = mContext.getFilesDir().getPath();
return mContext.openFileOutput(fileName , Context.MODE_WORLD_READABLE);
}
}
mSavePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File file = new File(mSavePath+"/filename.mp3");