我有示例应用程序,我想从 SD 卡中的某个位置加载图像,而不是从资产中加载图像,就像在示例中一样。但我不知道,我怎么能做到这一点。
此类包含来自资产的负载。
public class GalleryFileActivity extends Activity {
private GalleryViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] urls = null;
List<String> items = new ArrayList<String>();
try {
File dir = new File("/mnt/sdcard/images/");
File file[]=dir.listFiles();
urls = getAssets().list("");
File sdCard = Environment.getExternalStorageDirectory();
File images = new File (sdCard.getAbsolutePath() + "/foto");
File[] imagelist = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
urls = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
urls[i] = imagelist[i].getAbsolutePath();
}
} catch (IOException e) {
e.printStackTrace();
}
FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
{
@Override
public void onItemChange(int currentPosition)
{
Toast.makeText(GalleryFileActivity.this, "Current item is " + currentPosition, Toast.LENGTH_SHORT).show();
}
});
mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(pagerAdapter);
}
public void copy(InputStream in, File dst) throws IOException {
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
主班。
public class MainActivity extends ListActivity {
public static final String TITLE = "title";
public static final String SUBTITLE = "subtitle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
onListItemClick();
setListAdapter(createAdapter());
}
protected Map<String, String> createElement(String title, String subtitle)
{
Map<String, String> result = new HashMap<String, String>();
result.put(TITLE, title);
result.put(SUBTITLE, subtitle);
return result;
}
public List<Map<String, String>> getData()
{
List<Map<String, String>> result = new ArrayList<Map<String,String>>();
result.add(createElement("Local load", "In this example, you provide list of files to display in gallery"));
return result;
}
public ListAdapter createAdapter()
{
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_2,
new String[]{TITLE, SUBTITLE},
new int[]{android.R.id.text1, android.R.id.text2});
return adapter;
}
protected void onListItemClick() {
Intent i = null;
i = new Intent(this, GalleryFileActivity.class);
startActivity(i);
}
}
我认为这并不重要,但为了安全起见 XML。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.truba.touchgallery"
android:versionCode="2"
android:versionName="1.2"
android:installLocation="auto" >
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="false" >
<activity
android:name="ru.truba.touchgallery.GalleryUrlActivity"
android:configChanges="orientation" >
</activity>
<activity
android:name="ru.truba.touchgallery.GalleryFileActivity"
android:configChanges="orientation" >
</activity>
<activity
android:name="ru.truba.touchgallery.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在模拟器上运行,(文件夹 foto 不存在),但我认为错误在另一个地方。
FATAL EXCEPTION: main
09-09 18:10:49.968: E/AndroidRuntime(12289): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.truba.touchgallery/ru.truba.touchgallery.GalleryFileActivity}: java.lang.NullPointerException
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.os.Looper.loop(Looper.java:137)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-09 18:10:49.968: E/AndroidRuntime(12289): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:10:49.968: E/AndroidRuntime(12289): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 18:10:49.968: E/AndroidRuntime(12289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 18:10:49.968: E/AndroidRuntime(12289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-09 18:10:49.968: E/AndroidRuntime(12289): at dalvik.system.NativeStart.main(Native Method)
09-09 18:10:49.968: E/AndroidRuntime(12289): Caused by: java.lang.NullPointerException
09-09 18:10:49.968: E/AndroidRuntime(12289): at ru.truba.touchgallery.GalleryFileActivity.onCreate(GalleryFileActivity.java:63)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.Activity.performCreate(Activity.java:5104)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-09 18:10:49.968: E/AndroidRuntime(12289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-09 18:10:49.968: E/AndroidRuntime(12289): ... 11 more