I am uploading a image from gallery and saving it into new directory after cropping it (using Android-image-cropper library).
The directory is getting created and image is getting saved successfully. It can be found in File Explorer, but when I try upload the same image in the app it is not showing up.
I also tried saving the image in existing directory, the directory is visible in the search and not the image in it, but it can be found in File Explorer.
EditActivity
save_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveImage();
}
});
}
public void saveImage(){
try {
InputStream inputStream = getContentResolver().openInputStream(resultUri);
bitmap = BitmapFactory.decodeStream(inputStream);;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File file = Environment.getExternalStorageDirectory();
File dir = new File(file.getAbsolutePath()+"/Pictures/");
if(!dir.exists()){
dir.mkdirs();
}
File f = new File(dir,System.currentTimeMillis()+".jpg");
try {
outputStream = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG,90,outputStream);
Toast.makeText(getApplicationContext(),f.toString(),Toast.LENGTH_SHORT).show();
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
MainActivity
private void cam_permission() {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
} else{
dispatchTakePictureIntent();
}
}
private void storage_permission(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
} else{
Intent gallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, GALLERY_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, "Camera Permission is required", Toast.LENGTH_SHORT).show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
else {
Toast.makeText(MainActivity.this, "Storage Permission is required", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
File file = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(MainActivity.this, EditActivity.class);
intent .putExtra("imageUri", contentUri.toString());
startActivity(intent);
}
}
if (requestCode == GALLERY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(MainActivity.this, EditActivity.class);
intent .putExtra("imageUri", contentUri.toString());
startActivity(intent);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />