package com.smartapps.ImageResolution;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Details extends ActionBarActivity {
File profile_pic_file;
SharedPreferences pref;
ImageView preview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
pref = getSharedPreferences("log_account", 0);
String user_name = pref.getString("user_name", null);
if (user_name == null)
// Creating the profile pic file
try {
File dir = getExternalFilesDir(null);
File _dir = new File(dir, "LocalStorage");
if (!_dir.exists()) _dir.mkdir();
profile_pic_file = new File(_dir, "profile.jpg");
if (!profile_pic_file.exists()) profile_pic_file.createNewFile();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
preview = (ImageView) findViewById(R.id.preview_image);
ImageButton image_select = (ImageButton) findViewById(R.id.image_select);
Button save_butn = (Button) findViewById(R.id.save_butn);
image_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent image_select = new Intent();
image_select.setAction(Intent.ACTION_PICK);
image_select.setType("image/*");
startActivityForResult(image_select, 1);
}
});// end of the select_image button
save_butn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}); // end of the Save Button
}
private String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(projection[0]);
String path = cursor.getString(column);
cursor.close();
return path;
}
@Override
protected void onActivityResult(int req_code, int res_code, Intent data) {
if (req_code == 1)
if (res_code == RESULT_OK) {
String path = getPath(data.getData());
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] bytes_data = new byte[(int) file.length()];
fis.read(bytes_data);fis.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes_data, 0, bytes_data.length, options);
int w = options.outWidth,h=options.outHeight, insampleSize = 1;
if(h>w){ insampleSize=getSample(h,false); }
else { insampleSize=getSample(w,true); }
if(insampleSize!=1){
options.inSampleSize=insampleSize;
options.inJustDecodeBounds = false;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap = BitmapFactory.decodeByteArray(bytes_data, 0, bytes_data.length, options);
bitmap.compress(Bitmap.CompressFormat.JPEG,25,bos);
FileOutputStream fos = new FileOutputStream(profile_pic_file);
fos.write(bos.toByteArray());fos.flush();fos.close();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
if (req_code == 2)
if (res_code == RESULT_OK) {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
Bitmap bm=data.getExtras().getParcelable("data");
bm.compress(Bitmap.CompressFormat.JPEG,25,bos);
try {
FileOutputStream fos=new FileOutputStream(profile_pic_file);
fos.write(bos.toByteArray());fos.flush();fos.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();}
finally {
preview.setImageBitmap(bm);
}
}
}
private int getSample(int x,boolean isWidth){
int u=1;
if(isWidth)
{
if(x>1000) u=x/1000;
}
else{
if(x>350) u=x/350;
}
return u;
}
}