0

私のxmlコードは;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click To Download File"
        android:id="@+id/downloadButton"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/messageText"
        android:textColor="#000000"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</LinearLayout>

私のクラスは;

public class DownloadFromServer extends Activity {

TextView messageText;
Button downloadButton;
int serverResponseCode = 0;
ProgressDialog dialog = null;
String downloadServerUri = null;
Drawable drawable;
final String downloadFileName = "share.png";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download_from_server);

    downloadButton = (Button) findViewById(R.id.downloadButton);
    messageText = (TextView) findViewById(R.id.messageText);
    final ImageView image = (ImageView) findViewById(R.id.image);

    downloadServerUri = "http://www.androidexample.com/media/uploads/"+downloadFileName;

    downloadButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(DownloadFromServer.this, "", "Downloading File...",true);

            new Thread(new Runnable(){
                public void run(){
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            messageText.setText("downloading started....");                             
                        }
                    });

                    downloadFile(downloadServerUri, downloadFileName);
                    image.setImageDrawable(drawable);
                }
            }).start();
        }
    });       
}
public void downloadFile(String sourceFileUri, String fileName){

    try{
        File root = android.os.Environment.getExternalStorageDirectory(); 
        File dir = new File (root.getAbsolutePath());
        if(dir.exists()==false) {
            dir.mkdirs();
        }

        URL url = new URL(sourceFileUri); //you can write here any link
        File file = new File(dir, fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager", "download begining");
        Log.d("DownloadManager", "download url:" + url);
        Log.d("DownloadManager", "downloaded file name:" + fileName);

        URLConnection ucon = url.openConnection();
        ucon.setUseCaches(true);
        drawable = Drawable.createFromStream(ucon.getInputStream(), "image1");
        ucon.connect();

        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
           baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
        dialog.dismiss();
    }
    catch (IOException e) {
           Log.d("DownloadManager", "Error: " + e);
           dialog.dismiss();
    }

}

}

share.png という名前のサーバーからアップロードされた画像を取得しようとしましたが、その画像を ImageView の背景として使用したいと考えています。しかし、私の問題は、どのスレッドでも setImageDrawable() または setBackground() を使用できないことです。プログラムは、「ビュー階層を作成した元のスレッドのみがこのビューにアクセスできます」というエラーで失敗します。 どうすればそのエラーを修正できますか。どうもありがとう。

4

3 に答える 3

0
loadImage(url);



void loadImage(String image_location) {

    URL imageURL = null;
    if (image_location != null) {
        try {
            imageURL = new URL(image_location);
        }

        catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection connection = (HttpURLConnection) imageURL
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();

            bitmap = BitmapFactory.decodeStream(inputStream);// Convert to
                                                                // bitmap
            ivdpfirst.setImageBitmap(bitmap);
        } catch (IOException e) {

            e.printStackTrace();
        }
    } else {
        ivdpfirst.setImageDrawable(getResources().getDrawable(
                R.id.ivdpfirst));

    }
}
于 2013-08-12T13:07:18.980 に答える
0

不可能なバックグラウンド スレッドから ui を更新/アクセスしています。ui スレッドで ui を更新/アクセスする必要があります。

内側に移動runOnUiThread

      image.setImageDrawable(drawable);

AsyncTask混乱を避けるためにも使用できます。

onPreExecuteおよびを使用しonPostExecuteて UI を更新します。でダウンロードを行いdoInBackgroundます。計算結果doInBackgroundは param toonPostExecuteです。結果を受け取り、onPostExecuteUI を更新します。

AsyncTask ドキュメント

http://developer.android.com/reference/android/os/AsyncTask.html

編集:

public class MainActivity extends Activity {

TextView messageText;
Button downloadButton;
int serverResponseCode = 0;
ProgressDialog dialog = null;
String downloadServerUri = null;
Drawable drawable;
final String downloadFileName = "share.png";
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    downloadButton = (Button) findViewById(R.id.downloadButton);
    messageText = (TextView) findViewById(R.id.messageText);
    image = (ImageView) findViewById(R.id.image);

    downloadServerUri = "http://www.androidexample.com/media/uploads/"+downloadFileName;

    downloadButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            new DownLoadTask().execute();
        }
    });       
}

class DownLoadTask extends AsyncTask<Void,Void,Void>
{


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = ProgressDialog.show(MainActivity.this, "", "Downloading File...",true);
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        downloadFile(downloadServerUri, downloadFileName);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();
        image.setImageDrawable(drawable);
    }

}
public void downloadFile(String sourceFileUri, String fileName){

    try{
        File root = android.os.Environment.getExternalStorageDirectory(); 
        File dir = new File (root.getAbsolutePath());
        if(dir.exists()==false) {
            dir.mkdirs();
        }

        URL url = new URL(sourceFileUri); //you can write here any link
        File file = new File(dir, fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager", "download begining");
        Log.d("DownloadManager", "download url:" + url);
        Log.d("DownloadManager", "downloaded file name:" + fileName);

        URLConnection ucon = url.openConnection();
        ucon.setUseCaches(true);
        drawable = Drawable.createFromStream(ucon.getInputStream(), "image1");
        ucon.connect();

        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
           baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
        dialog.dismiss();
    }
    catch (IOException e) {
           Log.d("DownloadManager", "Error: " + e);
           dialog.dismiss();
    }

}
}

スナップショット

ここに画像の説明を入力

于 2013-08-12T13:07:02.993 に答える