简体   繁体   中英

Change address for load images to Gallery application

I have sample application, and I want to load images from some place in SD card, no from Assets, like in sample. But I don´t know, how I can do that.

This class contains load from Assets.

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();
}

}

The main class.

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);
}

}

I think is not important, but for to be on the safe side 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>

Run on emulator, (folder foto doesn't exist), but I think error is in another place.

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

EDIT:

edit 2:

File sdCard = Environment.getExternalStorageDirectory();

File images = new File (sdCard.getAbsolutePath() + "/foto");


  File[] imagelist = images.listFiles(new FilenameFilter(){  
        @override  
        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();  
        }  

Edit: (Manifest) add this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

change this part and put it like this:

 <activity
        android:name="GalleryFileActivity"
        android:configChanges="orientation" >
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM