0
我已經實現了使用GridView顯示圖像縮略圖和全屏活動的圖像庫,它將以全屏模式顯示每個圖像。使用Intents(putExtra)將文件路徑傳遞到GridView活動,以根據SDCARD中的文件夾名稱顯示不同的GridView。現在,我遇到的問題是,我無法將這些文件路徑傳遞到全屏活動以分別查看每個圖像。從Android獲取上下文Activiy
以下是我的課程;
MainActivity.java
package info.androidhive.imageslider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button btn1, btn2;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
setOnClickListeners();
}
public void setOnClickListeners(){
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
Toast.makeText(getApplicationContext(), "Loading Gridview 1", Toast.LENGTH_LONG).show();
intent = new Intent(MainActivity.this, GridViewActivity.class);
intent.putExtra("folder", "folder1");
startActivity(intent);
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Loading Gridview 2", Toast.LENGTH_LONG).show();
intent = new Intent(MainActivity.this, GridViewActivity.class);
intent.putExtra("folder", "folder2");
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
所以在這個活動想過去不同的文件夾名稱爲每個按鈕的點擊。
GridViewActivity.java
這個類將收到的意圖參數文件夾名稱,它會列出所有圖像的縮略圖爲特定的文件夾。直到這裏一切都很好,我可以根據點擊的按鈕查看每個不同文件夾的圖像。現在
package info.androidhive.imageslider;
import info.androidhive.imageslider.adapter.GridViewImageAdapter;
import info.androidhive.imageslider.helper.AppConstant;
import info.androidhive.imageslider.helper.Utils;
import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.Toast;
public class GridViewActivity extends Activity {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
//Utils
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
gridView = (GridView) findViewById(R.id.grid_view);
//utils = new Utils(this);
mContext = this;
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths,
columnWidth);
// setting grid view adapter
gridView.setAdapter(adapter);
}
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding))/AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
/*
* Reading file paths from SDCard // Utils
*/
public ArrayList<String> getFilePaths() {
ArrayList<String> filePaths = new ArrayList<String>();
String folder = getIntent().getStringExtra("folder");
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + folder);
// check for directory
if (directory.isDirectory()) {
// getting list of file paths
File[] listFiles = directory.listFiles();
// Check for count
if (listFiles.length > 0) {
// loop through all files
for (int i = 0; i < listFiles.length; i++) {
// get file path
String filePath = listFiles[i].getAbsolutePath();
// check for supported file extension
if (IsSupportedFile(filePath)) {
// Add image path to array list
filePaths.add(filePath);
}
}
} else {
// image directory is empty
Toast.makeText(
mContext,
folder
+ " is empty. Please load some images in it !",
Toast.LENGTH_LONG).show();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("Error!");
alert.setMessage(folder
+ " directory path is not valid! Please set the image directory name AppConstant.java class");
alert.setPositiveButton("OK", null);
alert.show();
}
return filePaths;
}
/*
* Check supported file extensions
*
* @returns boolean
*/
private boolean IsSupportedFile(String filePath) {
String ext = filePath.substring((filePath.lastIndexOf(".") + 1),
filePath.length());
if (AppConstant.FILE_EXTN
.contains(ext.toLowerCase(Locale.getDefault())))
return true;
else
return false;
}
/*
* getting screen width
*/
@SuppressLint("NewApi")
public int getScreenWidth() {
int columnWidth;
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}
}
當我點擊一個圖像縮略圖我(FullScreenView活動)不能拾取文件夾的名稱,所以我可以以全屏模式顯示圖像。
FullScreenViewActivity.java
package info.androidhive.imageslider;
import info.androidhive.imageslider.adapter.FullScreenImageAdapter;
//import info.androidhive.imageslider.helper.Utils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class FullScreenViewActivity extends Activity{
private GridViewActivity gv;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (ViewPager) findViewById(R.id.pager);
//utils = new GridViewActivity(getApplicationContext());
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
gv.getFilePaths());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
}
我想知道我怎麼能接受我的FullScreenActivity文件的參數?
尼斯一個我的朋友:大拇指! – AndroidKrayze