mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Image are fetched online
This commit is contained in:
parent
daa2308626
commit
08686391dc
@ -7,12 +7,14 @@ import android.support.v7.app.AppCompatActivity;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.api.APIRequestTask;
|
import org.communiquons.android.comunic.client.api.APIRequestTask;
|
||||||
import org.communiquons.android.comunic.client.data.Account.Account;
|
import org.communiquons.android.comunic.client.data.Account.Account;
|
||||||
import org.communiquons.android.comunic.client.data.Account.AccountUtils;
|
import org.communiquons.android.comunic.client.data.Account.AccountUtils;
|
||||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||||
|
import org.communiquons.android.comunic.client.data.ImageLoadTask;
|
||||||
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
|
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
|
||||||
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
||||||
|
|
||||||
@ -50,6 +52,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
//DEVELOPMENT : Try to get information about a user over the network
|
//DEVELOPMENT : Try to get information about a user over the network
|
||||||
GetUsersInfos uInfos = new GetUsersInfos(this, new DatabaseHelper(this));
|
GetUsersInfos uInfos = new GetUsersInfos(this, new DatabaseHelper(this));
|
||||||
|
final ImageView imageView = (ImageView) findViewById(R.id.test_img);
|
||||||
|
|
||||||
//Get infos... about me! :)
|
//Get infos... about me! :)
|
||||||
final int uID = aUtils.get_current_user_id();
|
final int uID = aUtils.get_current_user_id();
|
||||||
@ -60,6 +63,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
Toast.makeText(MainActivity.this, "Failure !", Toast.LENGTH_SHORT).show();
|
Toast.makeText(MainActivity.this, "Failure !", Toast.LENGTH_SHORT).show();
|
||||||
else {
|
else {
|
||||||
Toast.makeText(MainActivity.this, uID + " is " + info.getFullName() + "!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(MainActivity.this, uID + " is " + info.getFullName() + "!", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
new ImageLoadTask(MainActivity.this, info.getAcountImageURL(), imageView).execute();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -0,0 +1,130 @@
|
|||||||
|
package org.communiquons.android.comunic.client.data;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web image loader and renderer
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
* Created by pierre on 11/8/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ImageLoadTask extends AsyncTask<Void, Void, Void> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URL pointing on the image
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The target image view
|
||||||
|
*/
|
||||||
|
private ImageView view;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context of execution of the request
|
||||||
|
*/
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main folder in the cache directory that stores the file
|
||||||
|
*/
|
||||||
|
private final String IMAGE_CACHE_DIRECTORY = "img_cache/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Image file object
|
||||||
|
*/
|
||||||
|
private File img_file = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param context The context of the request, in order to be able to access the cache directory
|
||||||
|
* @param url The URL of the image to display
|
||||||
|
* @param view The target image view for the image
|
||||||
|
*/
|
||||||
|
public ImageLoadTask(Context context, String url, ImageView view){
|
||||||
|
//Save the values
|
||||||
|
this.mContext = context;
|
||||||
|
this.url = url;
|
||||||
|
this.view = view;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Background task
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... param){
|
||||||
|
|
||||||
|
//Determine the file name for the view
|
||||||
|
String filename = get_file_name(url);
|
||||||
|
if(filename == null){
|
||||||
|
Log.e("ImageLoadTask", "Couldn't generate file storage name !");
|
||||||
|
return null; //An error occured
|
||||||
|
}
|
||||||
|
String full_filename = IMAGE_CACHE_DIRECTORY + filename;
|
||||||
|
|
||||||
|
//Try to open the file
|
||||||
|
img_file = new File(mContext.getCacheDir(), full_filename);
|
||||||
|
|
||||||
|
|
||||||
|
//Check for errors
|
||||||
|
if(img_file == null){
|
||||||
|
Log.e("ImageLoadTask", "Couldn't open image file !");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
//Open the connection
|
||||||
|
URL urlObj = new URL(url);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
|
||||||
|
|
||||||
|
conn.setDoInput(true);
|
||||||
|
conn.setConnectTimeout(3000);
|
||||||
|
conn.setReadTimeout(3000);
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
//Get input stream
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
|
||||||
|
//Open the file for writing
|
||||||
|
OutputStream os = new FileOutputStream(img_file, false);
|
||||||
|
|
||||||
|
//Transfert bytes
|
||||||
|
Utilities.InputToOutputStream(is, os);
|
||||||
|
|
||||||
|
os.close();
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the file name, based on the URL name
|
||||||
|
*
|
||||||
|
* @param url The URL of the file
|
||||||
|
* @return The name of the file, composed of characters that can be used in filename
|
||||||
|
*/
|
||||||
|
private String get_file_name(String url){
|
||||||
|
return Utilities.sha1(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,13 @@ import android.util.Patterns;
|
|||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Formatter;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application utilities
|
* Application utilities
|
||||||
@ -104,4 +111,77 @@ public class Utilities {
|
|||||||
public boolean isValidMail(CharSequence mail){
|
public boolean isValidMail(CharSequence mail){
|
||||||
return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches();
|
return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the SHA-1 summary of a given string
|
||||||
|
*
|
||||||
|
* @param source The source string
|
||||||
|
* @return The SHA-1 encoded string
|
||||||
|
*/
|
||||||
|
public static String sha1(String source){
|
||||||
|
|
||||||
|
String sha1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
||||||
|
crypt.reset();
|
||||||
|
crypt.update(source.getBytes("UTF-8"));
|
||||||
|
sha1 = byteToHex(crypt.digest());
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (UnsupportedEncodingException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sha1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an array of bytes into a string
|
||||||
|
*
|
||||||
|
* @param bList The list of bytes
|
||||||
|
* @return The result string
|
||||||
|
*/
|
||||||
|
private static String byteToHex(byte[] bList){
|
||||||
|
|
||||||
|
Formatter formatter = new Formatter();
|
||||||
|
|
||||||
|
for(byte b : bList){
|
||||||
|
formatter.format("%02x", b);
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = formatter.toString();
|
||||||
|
formatter.close();
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transfer all the data coming from an InputStream to an Output Stream
|
||||||
|
*
|
||||||
|
* @param is The Input stream
|
||||||
|
* @param os The output stream
|
||||||
|
* @return The number of byte transfered
|
||||||
|
*/
|
||||||
|
public static int InputToOutputStream(InputStream is, OutputStream os){
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
int b = is.read();
|
||||||
|
while (b != -1){
|
||||||
|
os.write(b);
|
||||||
|
count++;
|
||||||
|
b = is.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,4 +15,14 @@
|
|||||||
app:layout_constraintRight_toRightOf="parent"
|
app:layout_constraintRight_toRightOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/test_img"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:layout_width="100dp"
|
||||||
|
tools:layout_height="100dp"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:layout_constraintLeft_toLeftOf="parent"
|
||||||
|
/>
|
||||||
|
|
||||||
</android.support.constraint.ConstraintLayout>
|
</android.support.constraint.ConstraintLayout>
|
||||||
|
Loading…
Reference in New Issue
Block a user