Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
serifgungor authored Jul 27, 2019
1 parent d4aac27 commit 92bc582
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
compile project(path: ':photto')
}
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gungoronline.photto">

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/gungoronline/photto/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gungoronline.photto;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
Expand All @@ -10,5 +9,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}
}
7 changes: 6 additions & 1 deletion photto/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gungoronline.photto" />
package="com.gungoronline.photto" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
97 changes: 91 additions & 6 deletions photto/src/main/java/com/gungoronline/photto/Model/Photto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.gungoronline.photto.Helper.ImageLibrary;

import java.io.File;
import java.io.InputStream;

public class Photto {
Expand All @@ -23,6 +27,7 @@ public class Photto {
private int drawableResourceId;
private InputStream inputStream;
private byte[] decodedString;
private File file;


public Photto(PhottoBuilder phottoBuilder) {
Expand All @@ -34,6 +39,11 @@ public Photto(PhottoBuilder phottoBuilder) {
this.drawableResourceId = phottoBuilder.drawableResourceId;
this.inputStream = phottoBuilder.inputStream;
this.decodedString = phottoBuilder.decodedString;
this.file = phottoBuilder.file;
}

public File getFile() {
return file;
}

public Uri getUri() {
Expand Down Expand Up @@ -82,10 +92,30 @@ public static class PhottoBuilder {
private int drawableResourceId;
private InputStream inputStream;
private byte[] decodedString;
private File file;

Photto photto;
ImageLibrary il;

public interface ImageLoadListener {
public void onImageLoaded();

public void onError(int errorCode);

public void onImageLoading();
}

private ImageLoadListener imageLoadListener;

public boolean networkConnection() {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}


private String reverseString(String str) {
String reverse = "";
Expand All @@ -98,17 +128,41 @@ private String reverseString(String str) {
public PhottoBuilder(Context context, String url, ImageView into) {
this.context = context;
this.url = url;
String str = reverseString(url.replaceAll("[^a-zA-Z0-9]", ""));
this.uniqueName = str.substring(60);
if (!url.isEmpty()) {
String str = reverseString(url.replaceAll("[^a-zA-Z0-9]", ""));
this.uniqueName = str.substring(60);
} else {
imageLoadListener.onError(2);
}
this.into = into;
}

public PhottoBuilder(Context context, String url, ImageLoadListener imageLoadListener, ImageView into) {
this.context = context;
this.url = url;
if (!url.isEmpty()) {
String str = reverseString(url.replaceAll("[^a-zA-Z0-9]", ""));
this.uniqueName = str.substring(60);
} else {
imageLoadListener.onError(2);
}
this.into = into;
this.imageLoadListener = imageLoadListener;
}


public PhottoBuilder(Context context, Uri uri, ImageView into) {
this.context = context;
this.uri = uri;
this.into = into;
}

public PhottoBuilder(Context context, File file, ImageView into) {
this.context = context;
this.file = file;
this.into = into;
}

public PhottoBuilder(Context context, int drawableResourceId, ImageView into) {
this.context = context;
this.drawableResourceId = drawableResourceId;
Expand Down Expand Up @@ -181,23 +235,54 @@ protected void onPostExecute(Void aVoid) {
if (photto.getUri() != null) {
into.setImageURI(photto.uri);
} else if (photto.getUrl() != null) {
into.setImageBitmap(il.loadImageFromStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg"));
if (!photto.getUrl().isEmpty()) {
if (networkConnection()) {
if (imageLoadListener != null) {
into.setImageBitmap(il.loadImageFromStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg"));
imageLoadListener.onImageLoaded();
}
} else {
Bitmap bp = il.loadImageFromStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg");
if (bp != null) {
into.setImageBitmap(bp);
}
}
}

} else if (photto.getDrawableResourceId() != 0) {
into.setImageResource(photto.drawableResourceId);
} else if (photto.getInputStream() != null) {
into.setImageDrawable(Drawable.createFromStream(inputStream, null));
} else if (photto.getDecodedString() != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
into.setImageBitmap(bitmap);
} else if (photto.getFile() != null) {
into.setImageBitmap(BitmapFactory.decodeFile(photto.getFile().getAbsolutePath()));
}

}

@Override
protected Void doInBackground(Void... voids) {
if (photto.getUrl() != null) {
if (il.isImageInStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg") == false) {
Bitmap bitmap = il.getBitmapFromURL(url);
il.saveImageToInternalStorage(bitmap, uniqueName + ".jpg", context.getApplicationContext());

if (!photto.getUrl().isEmpty()) {
if (networkConnection()) {
if (il.isImageInStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg") == false) {
Bitmap bitmap = il.getBitmapFromURL(url);
il.saveImageToInternalStorage(bitmap, uniqueName + ".jpg", context.getApplicationContext());
}
if (imageLoadListener != null) {
imageLoadListener.onImageLoading();
}
} else {
Bitmap bp = il.loadImageFromStorage("/data/data/" + context.getPackageName() + "/app_imageDir", uniqueName + ".jpg");
if (bp != null) {
imageLoadListener.onError(3);
} else {
imageLoadListener.onError(1);
}
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gungoronline.photto.Model;

public class PhottoError {
public static int NETWORK_ERROR = 1;
public static int URL_IS_BLANK = 2;
public static int NETWORK_ERROR_BUT_CACHE = 3;
}
11 changes: 11 additions & 0 deletions photto/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 92bc582

Please sign in to comment.