0

了解しました。https://github.com/nostra13/Android-Universal-Image-Loaderを参照してください。このアプリは、array.xml内のすべてのURLを一覧表示しています。http://getablogger.blogspot.com/2008/01/android-download-image-from-server-and.htmlのガイドに従って、array.xmlにURLを指定せずに画像を一覧表示することは可能ですか?サーバーIPを使用して、すべての画像を取得します。

getabloggerからのコード

ImageView imView;
String imageUrl="http://11.0.6.23/";
Random r;
/** Called when the activity is first created. */ 
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
r= new Random();

Button bt3= (Button)findViewById(R.id.get_imagebt);
bt3.setOnClickListener(getImgListener);
imView = (ImageView)findViewById(R.id.imview);
} 

View.OnClickListener getImgListener = new View.OnClickListener()
{

@Override
public void onClick(View view) {
// TODO Auto-generated method stub

//i tried to randomize the file download, in my server i put 4 files with name like
//png0.png, png1.png, png2.png so different file is downloaded in button press
int i =r.nextInt()%4;
downloadFile(imageUrl+"png"+i+".png");
Log.i("im url",imageUrl+"png"+i+".png");
}

};

ユニバーサルイメージローダーのコード

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="heavy_images">
        <item>https://lh6.googleusercontent.com/-jZgveEqb6pg/T3R4kXScycI/AAAAAAAAAE0/xQ7CvpfXDzc/s1024/sample_image_01.jpg</item>
        <item>https://lh4.googleusercontent.com/-K2FMuOozxU0/T3R4lRAiBTI/AAAAAAAAAE8/a3Eh9JvnnzI/s1024/sample_image_02.jpg</item>
        <item>https://lh5.googleusercontent.com/-SCS5C646rxM/T3R4l7QB6xI/AAAAAAAAAFE/xLcuVv3CUyA/s1024/sample_image_03.jpg</item>
        <item>https://lh6.googleusercontent.com/-f0NJR6-_Thg/T3R4mNex2wI/AAAAAAAAAFI/45oug4VE8MI/s1024/sample_image_04.jpg</item>
        <item>https://lh3.googleusercontent.com/-n-xcJmiI0pg/T3R4mkSchHI/AAAAAAAAAFU/EoiNNb7kk3A/s1024/sample_image_05.jpg</item>
        <item>https://lh3.googleusercontent.com/-X43vAudm7f4/T3R4nGSChJI/AAAAAAAAAFk/3bna6D-2EE8/s1024/sample_image_06.jpg</item>
        <item>https://lh5.googleusercontent.com/-MpZneqIyjXU/T3R4nuGO1aI/AAAAAAAAAFg/r09OPjLx1ZY/s1024/sample_image_07.jpg</item>
        <item>https://lh6.googleusercontent.com/-ql3YNfdClJo/T3XvW9apmFI/AAAAAAAAAL4/_6HFDzbahc4/s1024/sample_image_08.jpg</item>
        <item>https://lh5.googleusercontent.com/-Pxa7eqF4cyc/T3R4oasvPEI/AAAAAAAAAF0/-uYDH92h8LA/s1024/sample_image_09.jpg</item>
        <item>https://lh4.googleusercontent.com/-Li-rjhFEuaI/T3R4o-VUl4I/AAAAAAAAAF8/5E5XdMnP1oE/s1024/sample_image_10.jpg</item>
        <item>https://lh5.googleusercontent.com/-_HU4fImgFhA/T3R4pPVIwWI/AAAAAAAAAGA/0RfK_Vkgth4/s1024/sample_image_11.jpg</item>
        <item>https://lh6.googleusercontent.com/-0gnNrVjwa0Y/T3R4peGYJwI/AAAAAAAAAGU/uX_9wvRPM9I/s1024/sample_image_12.jpg</item>
        <item>https://lh3.googleusercontent.com/-HBxuzALS_Zs/T3R4qERykaI/AAAAAAAAAGQ/_qQ16FaZ1q0/s1024/sample_image_13.jpg</item>
        <item>https://lh4.googleusercontent.com/-cKojDrARNjQ/T3R4qfWSGPI/AAAAAAAAAGY/MR5dnbNaPyY/s1024/sample_image_14.jpg</item>
        <item>https://lh3.googleusercontent.com/-WujkdYfcyZ8/T3R4qrIMGUI/AAAAAAAAAGk/277LIdgvnjg/s1024/sample_image_15.jpg</item>
        <item>https://lh6.googleusercontent.com/-FMHR7Vy3PgI/T3R4rOXlEKI/AAAAAAAAAGs/VeXrDNDBkaw/s1024/sample_image_16.jpg</item>
        <item>https://lh4.googleusercontent.com/-mrR0AJyNTH0/T3R4rZs6CuI/AAAAAAAAAG0/UE1wQqCOqLA/s1024/sample_image_17.jpg</item>
        <item>https://lh6.googleusercontent.com/-z77w0eh3cow/T3R4rnLn05I/AAAAAAAAAG4/BaerfWoNucU/s1024/sample_image_18.jpg</item>
        <item>https://lh5.googleusercontent.com/-aWVwh1OU5Bk/T3R4sAWw0yI/AAAAAAAAAHE/4_KAvJttFwA/s1024/sample_image_19.jpg</item>
        <item>https://lh6.googleusercontent.com/-q-js52DMnWQ/T3R4tZhY2sI/AAAAAAAAAHM/A8kjp2Ivdqg/s1024/sample_image_20.jpg</item>
        <item>https://lh5.googleusercontent.com/-_jIzvvzXKn4/T3R4t7xpdVI/AAAAAAAAAHU/7QC6eZ10jgs/s1024/sample_image_21.jpg</item>
        <item>https://lh3.googleusercontent.com/-lnGi4IMLpwU/T3R4uCMa7vI/AAAAAAAAAHc/1zgzzz6qTpk/s1024/sample_image_22.jpg</item>
        <item>https://lh5.googleusercontent.com/-fFCzKjFPsPc/T3R4u0SZPFI/AAAAAAAAAHk/sbgjzrktOK0/s1024/sample_image_23.jpg</item>
        <item>https://lh4.googleusercontent.com/-8TqoW5gBE_Y/T3R4vBS3NPI/AAAAAAAAAHs/EZYvpNsaNXk/s1024/sample_image_24.jpg</item>
        <item>https://lh6.googleusercontent.com/-gc4eQ3ySdzs/T3R4vafoA7I/AAAAAAAAAH4/yKii5P6tqDE/s1024/sample_image_25.jpg</item>
        <item>https://lh5.googleusercontent.com/--NYOPCylU7Q/T3R4vjAiWkI/AAAAAAAAAH8/IPNx5q3ptRA/s1024/sample_image_26.jpg</item>
        <item>https://lh6.googleusercontent.com/-9IJM8so4vCI/T3R4vwJO2yI/AAAAAAAAAIE/ljlr-cwuqZM/s1024/sample_image_27.jpg</item>
        <item>https://lh4.googleusercontent.com/-KW6QwOHfhBs/T3R4w0RsQiI/AAAAAAAAAIM/uEFLVgHPFCk/s1024/sample_image_28.jpg</item>
        <item>https://lh4.googleusercontent.com/-z2557Ec1ctY/T3R4x3QA2hI/AAAAAAAAAIk/9-GzPL1lTWE/s1024/sample_image_29.jpg</item>
        <item>https://lh5.googleusercontent.com/-LaKXAn4Kr1c/T3R4yc5b4lI/AAAAAAAAAIY/fMgcOVQfmD0/s1024/sample_image_30.jpg</item>
        <item>https://lh4.googleusercontent.com/-F9LRToJoQdo/T3R4yrLtyQI/AAAAAAAAAIg/ri9uUCWuRmo/s1024/sample_image_31.jpg</item>
        <item>https://lh4.googleusercontent.com/-6X-xBwP-QpI/T3R4zGVboII/AAAAAAAAAIs/zYH4PjjngY0/s1024/sample_image_32.jpg</item>
        <item>https://lh5.googleusercontent.com/-VdLRjbW4LAs/T3R4zXu3gUI/AAAAAAAAAIw/9aFp9t7mCPg/s1024/sample_image_33.jpg</item>
        <item>https://lh6.googleusercontent.com/-gL6R17_fDJU/T3R4zpIXGjI/AAAAAAAAAI8/Q2Vjx-L9X20/s1024/sample_image_34.jpg</item>
        <item>https://lh3.googleusercontent.com/-1fGH4YJXEzo/T3R40Y1B7KI/AAAAAAAAAJE/MnTsa77g-nk/s1024/sample_image_35.jpg</item>
        <item>https://lh4.googleusercontent.com/-Ql0jHSrea-A/T3R403mUfFI/AAAAAAAAAJM/qzI4SkcH9tY/s1024/sample_image_36.jpg</item>
        <item>https://lh5.googleusercontent.com/-BL5FIBR_tzI/T3R41DA0AKI/AAAAAAAAAJk/GZfeeb-SLM0/s1024/sample_image_37.jpg</item>
        <item>https://lh4.googleusercontent.com/-wF2Vc9YDutw/T3R41fR2BCI/AAAAAAAAAJc/JdU1sHdMRAk/s1024/sample_image_38.jpg</item>
        <item>https://lh6.googleusercontent.com/-ZWHiPehwjTI/T3R41zuaKCI/AAAAAAAAAJg/hR3QJ1v3REg/s1024/sample_image_39.jpg</item>
    </string-array>
    <string-array name="light_images">
        <item>http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png</item>
        <item>http://simpozia.com/pages/images/stories/windows-icon.png</item>
        <item>https://si0.twimg.com/profile_images/1135218951/gmail_profile_icon3_normal.png</item>
        <item>http://www.krify.net/wp-content/uploads/2011/09/Macromedia_Flash_dock_icon.png</item>
        <item>http://radiotray.sourceforge.net/radio.png</item>
        <item>http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png</item>
        <item>http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png</item>
        <item>http://weloveicons.s3.amazonaws.com/icons/100929_applications.png</item>
        <item>http://t2.gstatic.com/images?q=tbn:ANd9GcTJixLIo_zlOPOILuxNWc5evK333pZCH8rugaTtv3SZSfiI39T0-3vWYQ</item>
        <item>http://www.idyllicmusic.com/index_files/get_apple-iphone.png</item>
        <item>http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png</item>
        <item>http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png</item>
        <item>http://www.desiredsoft.com/images/icon_webhosting.png</item>
        <item>http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png</item>
        <item>http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png</item>
        <item>http://cdn3.iconfinder.com/data/icons/transformers/computer.png</item>
        <item>http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe</item>
        <item>https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba</item>
        <item>http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg</item>
        <item>http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png</item>
        <item>http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png</item>
        <item>http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png</item>
        <item>http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe</item>
        <item>http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png</item>
        <item>http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png</item>
        <item>http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f</item>
        <item>http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg</item>
        <!-- Special cases -->
        <item>file:///mnt/sdcard/Temp/UniversalImageLoader.png</item> <!-- Local image -->
        <item>http://upload.wikimedia.org/wikipedia/ru/b/b6/Как_кот_с_мышами_воевал.png</item> <!-- Link with special UTF-8 symbols -->
        <item>https://www.iabti.org/images/M_images/Twitter_Image/follow_twitter_button_d.png</item> <!-- Image from HTTPS -->
        <item>http://bit.ly/soBiXr</item> <!-- Redirect link -->
        <item></item> <!-- Empty link -->
        <item>http://wrong.site.com/corruptedLink</item> <!-- Wrong link -->
    </string-array>

</resources>
4

2 に答える 2

2

これらのURLを配列に保存し、そのIDを呼び出す代わりに、必要な場所で呼び出すだけですheavy_image(これは何かになりますR.id.something)。

于 2012-09-12T10:48:56.180 に答える
0

次に、これを試してください-

private static final String TAG = "Image";
@SuppressWarnings("unused")
private boolean isImage = false;
private String reviewImageLink;

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

    reviewImageLink = getString(R.string.ImageURI);   // ImageURL
    URL reviewImageURL;
    String name = reviewImageLink.substring(reviewImageLink
            .lastIndexOf("/") + 1);
    try {
        reviewImageURL = new URL(reviewImageLink);
        if (!hasExternalStoragePublicPicture(name)) {
            isImage = false;
            new DownloadImageTask().execute(reviewImageURL);
            Log.v("log_tag", "if");
            isImage = true;
            File sdImageMainDirectory = new File(Environment
                    .getExternalStorageDirectory(), getResources()
                    .getString(R.string.directory)); // Storing Directory
            sdImageMainDirectory.mkdirs();
            File file = new File(sdImageMainDirectory, name);
            Log.v("log_tag", "Directory created");
        }

    } catch (MalformedURLException e) {
        Log.v(TAG, e.toString());
    }

}


private class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
    // This class definition states that DownloadImageTask will take String
    // parameters, publish Integer progress updates, and return a Bitmap
    protected Bitmap doInBackground(URL... paths) {
        URL url;
        try {
            url = paths[0];
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            int length = connection.getContentLength();
            InputStream is = (InputStream) url.getContent();
            byte[] imageData = new byte[length];
            int buffersize = (int) Math.ceil(length / (double) 100);
            int downloaded = 0;
            int read;
            while (downloaded < length) {
                if (length < buffersize) {
                    read = is.read(imageData, downloaded, length);
                } else if ((length - downloaded) <= buffersize) {
                    read = is.read(imageData, downloaded, length
                            - downloaded);
                } else {
                    read = is.read(imageData, downloaded, buffersize);
                }
                downloaded += read;
                publishProgress((downloaded * 100) / length);
            }
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
                    length);
            if (bitmap != null) {
                Log.i(TAG, "Bitmap created");
            } else {
                Log.i(TAG, "Bitmap not created");
            }
            is.close();
            return bitmap;
        } catch (MalformedURLException e) {
            Log.e(TAG, "Malformed exception: " + e.toString());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.toString());
        }
        return null;

    }

    protected void onPostExecute(Bitmap result) {
        String name = reviewImageLink.substring(reviewImageLink
                .lastIndexOf("/") + 1);
        if (result != null) {
            hasExternalStoragePublicPicture(name);
            saveToSDCard(result, name);
            isImage = true;

        } else {
            isImage = false;

        }
    }
}

public void saveToSDCard(Bitmap bitmap, String name) {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
        Log.v(TAG, "SD Card is available for read and write "
                + mExternalStorageAvailable + mExternalStorageWriteable);
        saveFile(bitmap, name);
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
        Log.v(TAG, "SD Card is available for read "
                + mExternalStorageAvailable);
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
        Log.v(TAG, "Please insert a SD Card to save your Video "
                + mExternalStorageAvailable + mExternalStorageWriteable);
    }
}

private void saveFile(Bitmap bitmap, String name) {
    String filename = name;
    ContentValues values = new ContentValues();
    File sdImageMainDirectory = new File(Environment
            .getExternalStorageDirectory(), getResources().getString(
            R.string.directory));
    sdImageMainDirectory.mkdirs();
    File outputFile = new File(sdImageMainDirectory, filename);
    values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
    values.put(MediaStore.MediaColumns.TITLE, filename);
    values.put(MediaStore.MediaColumns.DATE_ADDED, System
            .currentTimeMillis());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
    Uri uri = this.getContentResolver().insert(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

            values);
    try {
        OutputStream outStream = this.getContentResolver()
                .openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);

        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private boolean hasExternalStoragePublicPicture(String name) {
    File sdImageMainDirectory = new File(Environment
            .getExternalStorageDirectory(), getResources().getString(
            R.string.directory));
    File file = new File(sdImageMainDirectory, name);
    if (file != null) {
        file.delete();
    }
    return file.exists();
}

私の場合、strings.xml

<string name="ImageURI">http://cdn.sstatic.net/stackoverflow/img/tag-logo-android.png</string>
<string name="directory">/DCIM</string>

これから、imageUrlを直接与えるreviewImageLinkか、Stringリソースを与えることができます。これも同じですdirectory

これがあなたに大いに役立つことを願っています。

于 2012-07-26T09:18:43.207 に答える