public void Camera()
{
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null)
{
File photoFile = null;
try
{
photoFile = createImageFile();
}
catch(IOException e)
{
Toast.makeText(this, "Camera Error", Toast.LENGTH_SHORT).show();
}
if(photoFile != null)
{
Uri providerUri = FileProvider.getUriForFile(this, getPackageName()+".fileprovider", photoFile);
imageUri = providerUri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, providerUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
else
{
Toast.makeText(this, "None createImageFile", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(this, "저장 공간 접근 불가", Toast.LENGTH_SHORT).show();
return;
}
------------------
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = timeStamp + ".jpg";
imageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/image/"+imageFileName);
if(!imageFile.exists())
{
imageFile.getParentFile().mkdirs();
imageFile.createNewFile();
}
mCurrentPhotoPath = imageFile.getAbsolutePath();
Toast.makeText(this, "mCurrentPhotoPath", Toast.LENGTH_SHORT).show();
Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_SHORT).show();
return imageFile;
}
------------------------------------
private void galleryAddPic()
{
final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(mCurrentPhotoPath);
contentUri = Uri.fromFile(file);
intent.setData(contentUri);
Log.i("Upload", "Upload Start");
storageRef = storage.getReferenceFromUrl("gs").child("images/"+imageFileName);
storageRef.putFile(contentUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();
downloadImageFile(intent);
Log.i("File Upload","File Upload Success");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("File Upload","File Upload Failed");
}
});
Toast.makeText(this, "사진 저장 완료", Toast.LENGTH_SHORT).show();
}
-------------------------------------------
private void downloadImageFile(final Intent intent)
{
StorageReference gsReference = storage.getReferenceFromUrl("gs");
StorageReference pathReference = gsReference.child("images/"+imageFileName);
try
{
File localFile = File.createTempFile("images", "jpg");
pathReference.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Download Success", Toast.LENGTH_SHORT).show();
Log.i("Download", "Download Success");
imageDownload = true;
setImageFile(intent);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle failed download
// ...
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
-------------------------------------------------------
public void setImageFile(Intent intent)
{
if(imageDownload == true)
{
bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
ExifInterface exif = null;
try {
exif = new ExifInterface(mCurrentPhotoPath);
} catch (IOException e) {
e.printStackTrace();
}
exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
exifDegree = exifOrientationToDegrees(exifOrientation);
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
imageView.setImageBitmap(rotate(bitmap, exifDegree));
if(bitmap != null)
{
Toast.makeText(this, "butmap memory release", Toast.LENGTH_SHORT).show();
bitmap.recycle();
bitmap = null;
}
imageDownload = false;
sendBroadcast(intent);
}
}