Last active
April 9, 2018 17:08
-
-
Save alitamoor65/20716a9c78844c91e5c1648a763b0d25 to your computer and use it in GitHub Desktop.
Grid View From Local Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
style="@style/AppTheme" | |
android:layout_width="fill_parent" | |
android:padding="5dp" | |
android:layout_height="fill_parent" | |
android:gravity="center_horizontal"> | |
<ImageView | |
android:background="#FFFFFFFF" | |
android:scaleType="fitXY" | |
android:layout_width="200dp" | |
android:layout_height="200dp" | |
android:id="@+id/imageViewGrid" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout 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" | |
android:background="@drawable/main_bg" | |
tools:context="pk.camict.jovial.eidmubarakprofile.FrameChosserActivity" | |
android:gravity="center"> | |
<GridView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/grid_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:numColumns="auto_fit" | |
android:stretchMode="columnWidth" | |
android:horizontalSpacing="5dp" | |
android:verticalSpacing="5dp" | |
android:gravity="center"> | |
</GridView> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pk.camict.jovial.eidmubarakprofile; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.view.KeyEvent; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.GridView; | |
import android.widget.Toast; | |
import java.io.File; | |
public class FrameChosserActivity extends Activity { | |
GridViewAdapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_frame_chosser); | |
GridView gridview = findViewById(R.id.grid_view); | |
adapter = new GridViewAdapter(this); | |
gridview.setAdapter(adapter); | |
String ExternalStorageDirectoryPath = "data/data/" + this.getPackageName(); | |
String targetPath = ExternalStorageDirectoryPath + "/Frames"; | |
//Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show(); | |
File targetDirector = new File(targetPath); | |
File[] files = targetDirector.listFiles(); | |
for (File file : files){ | |
adapter.add(file.getAbsolutePath()); | |
} | |
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
String imgPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FilterImages/IMAGE-TEMP.png"; | |
String filterPath = adapter.getItemPath(position); | |
Toast.makeText(FrameChosserActivity.this, "Frame Selected.\nPlease Wait...", Toast.LENGTH_SHORT).show(); | |
//Toast.makeText(FrameChosserActivity.this, "Item: " + adapter.getItemPath(position), Toast.LENGTH_SHORT).show(); | |
Intent intent=new Intent(FrameChosserActivity.this, MainActivity.class); | |
Bundle bundle = new Bundle(); | |
bundle.putString("IMG_PATH",imgPath); | |
bundle.putString("FILTER_PATH",filterPath); | |
intent.putExtras(bundle); | |
startActivity(intent); | |
finish(); | |
} | |
}); | |
} | |
private int pressedTime = 1; | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { | |
switch (event.getAction()) { | |
case KeyEvent.ACTION_DOWN: | |
if (pressedTime > 1) { | |
Intent i = new Intent(FrameChosserActivity.this, PhotoChosserActivity.class); | |
startActivity(i); | |
finish(); | |
} else { | |
Toast.makeText(getApplicationContext(), "Save Your Image Before Exit", | |
Toast.LENGTH_SHORT).show(); | |
pressedTime++; | |
} | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pk.camict.jovial.eidmubarakprofile; | |
/** | |
* Created by AliTamoor on 8/13/2017. | |
*/ | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.GridView; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.util.ArrayList; | |
public class GridViewAdapter extends BaseAdapter { | |
private Context mContext; | |
ArrayList<String> itemList = new ArrayList<String>(); | |
public GridViewAdapter(Context c) { | |
this.mContext = c; | |
} | |
void add(String path){ | |
itemList.add(path); | |
} | |
@Override | |
public int getCount() { | |
return itemList.size(); | |
} | |
@Override | |
public Object getItem(int arg0) { | |
return itemList.get(arg0); | |
} | |
public String getItemPath(int pos){ | |
return itemList.get(pos); | |
} | |
@Override | |
public long getItemId(int position) { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View grid; | |
ImageView imageView = null; | |
LayoutInflater inflater= (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
//if(convertView == null){ | |
grid = new View(mContext); | |
grid = inflater.inflate(R.layout.custom_gridview,null); | |
imageView = (ImageView)grid.findViewById(R.id.imageViewGrid); | |
//imageView.setScaleType(ImageView.ScaleType.FIT_XY); | |
//} | |
//else { | |
//grid = (View)convertView; | |
//} | |
// ImageView imageView; | |
// if (convertView == null) { // if it's not recycled, initialize some attributes | |
// imageView = new ImageView(mContext); | |
// imageView.setLayoutParams(new GridView.LayoutParams(220, 220)); | |
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); | |
// imageView.setPadding(8, 8, 8, 8); | |
// } else { | |
// imageView = (ImageView) convertView; | |
// } | |
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 200, 200); | |
imageView.setImageBitmap(bm); | |
return grid; | |
} | |
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { | |
Bitmap bm = null; | |
// First decode with inJustDecodeBounds=true to check dimensions | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(path, options); | |
// Calculate inSampleSize | |
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); | |
// Decode bitmap with inSampleSize set | |
options.inJustDecodeBounds = false; | |
bm = BitmapFactory.decodeFile(path, options); | |
return bm; | |
} | |
public int calculateInSampleSize( | |
BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
// Raw height and width of image | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) { | |
if (width > height) { | |
inSampleSize = Math.round((float)height / (float)reqHeight); | |
} else { | |
inSampleSize = Math.round((float)width / (float)reqWidth); | |
} | |
} | |
return inSampleSize; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment