.:: Jasa Membuat Aplikasi Website,Desktop,Android Order Now..!! | | Order Now..!! Jasa Membuat Project Arduino,Robotic,Print 3D ::.

Display photos from SD in android.widget.Gallery

0 komentar


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh

The photos in the selected folder will be loaded in a custom adapter to display in Gallery widget.

Modify main.xml, to add <Gallery>.










Modify AndroidGalleryActivity.java.

In order to reduce the resource need to keep the bitmaps, we have to implement a new class PhotoItem hold the path of individual jpg file, and the re-sized bitmap. And a custom BaseAdapter, PhotoBarAdapter, for PhotoItem is implemented for the Gallery.

 

package com.AndroidGallery;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidGalleryActivity extends Activity {

Gallery photoBar;

Button buttonOpenDialog;
Button buttonUp, buttonSelectFolder;
TextView jpgList;

String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;

ListView dialog_ListView;

File root;
File curFolder;

private List fileList = new ArrayList();

class PhotoItem{

String bitmapImageSrc;
Bitmap bitmapImage = null;

final static int itemWidth = 150;
final static int itemHeight = 150;

public PhotoItem(String src){
bitmapImageSrc = src;
bitmapImage = resize();
}

public Bitmap getImage(){
return bitmapImage;
}

private Bitmap resize(){
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeFile(bitmapImageSrc, bmpFactoryOptions);

int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)itemHeight);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)itemWidth);

if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio){
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}

bmpFactoryOptions.inJustDecodeBounds = false;

bitmap = BitmapFactory.decodeFile(bitmapImageSrc, bmpFactoryOptions);

return bitmap;
}
}

PhotoBarAdapter myPhotoBarAdapter;

public class PhotoBarAdapter extends BaseAdapter {

Context context;
ArrayList arrayPhotoItem;


PhotoBarAdapter(Context c){
context = c;
arrayPhotoItem = new ArrayList();
}

public void clear(){
arrayPhotoItem.clear();
}

public void addPhotoItem(PhotoItem item){
arrayPhotoItem.add(item);
}

@Override
public int getCount() {
return arrayPhotoItem.size();
}

@Override
public Object getItem(int position) {
return arrayPhotoItem.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LinearLayout viewLayout = new LinearLayout(context);
viewLayout.setLayoutParams(new Gallery.LayoutParams(200, 200));
viewLayout.setBackgroundColor(color.background_light);

ImageView imageView;
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(arrayPhotoItem.get(position).getImage());
//return imageView;

viewLayout.addView(imageView);
return viewLayout;
}

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

photoBar = (Gallery)findViewById(R.id.photobar);
myPhotoBarAdapter = new PhotoBarAdapter(this);
photoBar.setAdapter(myPhotoBarAdapter);

jpgList = (TextView)findViewById(R.id.jpglist);

buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});

root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());

curFolder = root;
//ListJpgInFolder(curFolder);
preparePhotoBarInFolder(curFolder);

}

@Override
protected Dialog onCreateDialog(int id) {

Dialog dialog = null;

switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(AndroidGalleryActivity.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Select JPG");

dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);

buttonUp = (Button)dialog.findViewById(R.id.up);
buttonUp.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ListDir(curFolder.getParentFile());
}});

buttonSelectFolder = (Button)dialog.findViewById(R.id.selectfolder);
buttonSelectFolder.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AndroidGalleryActivity.this,
curFolder + " selected",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);

//ListJpgInFolder(curFolder);
preparePhotoBarInFolder(curFolder);

}});


//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);

dialog_ListView.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
}
}});

break;
}
return dialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);

switch(id) {
case CUSTOM_DIALOG_ID:
ListDir(curFolder);

break;
}
}

void ListDir(File f){

if(f.equals(root)){
buttonUp.setEnabled(false);
}else{
buttonUp.setEnabled(true);
}

curFolder = f;
buttonSelectFolder.setText("Select Folder " + curFolder);

File[] files = f.listFiles();
fileList.clear();
for (File file : files){
if(file.isDirectory()){
fileList.add(file.getPath());
}else{
Uri selectedUri = Uri.fromFile(file);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
if(fileExtension.equalsIgnoreCase("jpg")){
fileList.add(file.getName());
}
}
}

ArrayAdapter directoryList
= new ArrayAdapter(this,
android.R.layout.simple_list_item_1, fileList);
dialog_ListView.setAdapter(directoryList);
}

private void preparePhotoBarInFolder(File folder){

String jpgs = "JPG fiels in folder " + folder.getAbsolutePath() + "\n\n";

File[] files = folder.listFiles();

myPhotoBarAdapter.clear();

for (File file : files){
if(!file.isDirectory()){
Uri selectedUri = Uri.fromFile(file);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
if(fileExtension.equalsIgnoreCase("jpg")){
jpgs += file.getAbsolutePath() + "\n";

PhotoItem pItem = new PhotoItem(file.getAbsolutePath());
myPhotoBarAdapter.addPhotoItem(pItem);

myPhotoBarAdapter.notifyDataSetChanged();
}
}
}
jpgList.setText(jpgs);
}

}
Display photos from SD in Gallery widget

Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Insyaallah Saya siap membantu, untuk Respon Cepat dapat menghubungi kami, melalui :

No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com


atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh


Artikel Display photos from SD in android.widget.Gallery , Diterbitkan oleh scodeaplikasi pada Minggu, 24 Februari 2013. Semoga artikel ini dapat menambah wawasan Anda. Website ini dipost dari beberapa sumber, bisa cek disini sumber, Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, karena segala yang dipost di public adalah milik public. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,Dengan Cara menghubungi saya Ke Email: Fajarudinsidik@gmail.com, atau No Hp/WA : (fajar) : 085369237896, (puat) : 085267792168.

Tawk.to