-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Added Picture Clicking Application
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
plugins { | ||
id 'com.android.application' | ||
} | ||
|
||
android { | ||
compileSdk 32 | ||
|
||
defaultConfig { | ||
applicationId "com.example.picclick" | ||
minSdk 23 | ||
targetSdk 32 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.appcompat:appcompat:1.5.1' | ||
implementation 'com.google.android.material:material:1.6.1' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | ||
implementation 'com.google.android.material:material:1.7.0-alpha03' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.picclick; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
assertEquals("com.example.picclick", appContext.getPackageName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.example.picclick"> | ||
|
||
<!--Permissions to use storage and camera--> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.CAMERA"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.PicClick" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<provider | ||
android:name="androidx.core.content.FileProvider" | ||
android:authorities="com.example.android.PicClick_https://github.com/jmanan088" | ||
android:exported="false" | ||
android:grantUriPermissions="true"> | ||
<meta-data | ||
android:name="android.support.FILE_PROVIDER_PATHS" | ||
android:resource="@xml/file_paths" /> | ||
</provider> | ||
</application> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.example.picclick; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.RequiresApi; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
import androidx.core.content.FileProvider; | ||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.icu.text.SimpleDateFormat; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.os.Environment; | ||
import android.provider.MediaStore; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.Toast; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
public static final int Camera_request = 101; | ||
public static final int Open_cam_req = 102; | ||
String currentPhotoPath; | ||
Button click, download; | ||
ImageView img; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
click = findViewById(R.id.click); | ||
download = findViewById(R.id.download); | ||
img = findViewById(R.id.img); | ||
// Disable state for downloading image before clicking photo, to prevent | ||
download.setEnabled(false); | ||
download.setVisibility(View.INVISIBLE); | ||
click.setOnClickListener(new View.OnClickListener() { | ||
// checking for minimum version required, N = Nougat | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
@Override | ||
public void onClick(View view) { | ||
//Asking camera permissions | ||
//Manifest.permission.CAMERA gets us the current situation for permissions and we match that with PackageManager.PERMISSION_GRANTED, which know is code for granted permission | ||
if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) { | ||
// We request permissions when our permission was not already granted. we are requesting permission within MainActivity as context, and every request needs i's own unique code, which here is Camera_request | ||
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, Camera_request); | ||
} | ||
else{ | ||
//if permission are given then call picture taking function | ||
dispatchTakePictureIntent(); | ||
} | ||
} | ||
}); | ||
download.setOnClickListener(new View.OnClickListener() { | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
@Override | ||
public void onClick(View view) { | ||
getImageUri(MainActivity.this,((BitmapDrawable)img.getDrawable()).getBitmap()); | ||
Toast.makeText(MainActivity.this, "Image Downloaded.", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
} | ||
|
||
// Handles the results for our permission requests | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
if(requestCode == Camera_request){ | ||
// requestCode== camera_request means we are camera permissions | ||
if(grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){ | ||
// If permission was granted then we start our camera taking function | ||
dispatchTakePictureIntent(); | ||
} | ||
else{ | ||
//permission not given | ||
Toast.makeText(this, "Camera permission required to use camera.", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
} | ||
|
||
//gets our result back from camera app and set that file we got to image view. | ||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if(requestCode==Open_cam_req){ | ||
if(resultCode == Activity.RESULT_OK){ | ||
File f = new File(currentPhotoPath); | ||
img.setImageURI(Uri.fromFile(f)); | ||
// set the visibility and clickable button to true after user has clicked the image to that he can download the image | ||
download.setVisibility(View.VISIBLE); | ||
download.setEnabled(true); | ||
} | ||
} | ||
} | ||
|
||
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
private File createImageFile() throws IOException { | ||
// Create an image file name | ||
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRENCH).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; | ||
} | ||
|
||
|
||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
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) { | ||
//authority i.e. provider name could be anything | ||
Uri photoURI = FileProvider.getUriForFile(this, | ||
"com.example.android.PicClick_https://github.com/jmanan088", | ||
photoFile); | ||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); | ||
startActivityForResult(takePictureIntent, Open_cam_req); | ||
} | ||
} | ||
} | ||
|
||
//Function to save image to external storage | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
public void getImageUri(Context inContext, Bitmap inImage) { | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); | ||
//Storing the photo with date-time format as name | ||
String timeStamp = new SimpleDateFormat("dd_MM_yyyy-HH.mm.ss", Locale.FRENCH).format(new Date()); | ||
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, timeStamp, null); | ||
} | ||
} |