0

在我的第一个活动中,我有一个按钮和一个 web 视图,当我单击该按钮时,它会打开一个新活动,其中包含图像视图中 web 视图的内容。(我在位图中转换 webview 并用这个位图设置 imageview)。

问题是当我第一次单击按钮时,什么也没有发生,我的屏幕保持白色。但是,如果我回到第一个活动并重新单击按钮,则图像视图会很好地显示。

为什么第一次没有显示imageview?我是否需要刷新我的第二个活动或 Imageview 之类的东西?

WebViewActivity.java

public class WebViewActivity extends Activity {

    private WebView webView;
    ImageView imageView;
    Button button;


    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.webview);

       webView = (WebView) findViewById(R.id.webView1);
       webView.getSettings().setJavaScriptEnabled(true);
       String customHtml = "<BODY BACKGROUND=\"pic_10.jpg\"><BODY><div style=\"position: absolute; bottom: 0; width: 100%\"><div style=\"padding:24px 0 0;margin:0;height:40px;background:transparent;position:relative;\"><a href=\"http://www.amazon.com\" style=\"padding:0 140px 0 0;margin:0;height:40px;display:block;text-decoration:none;overflow:hidden;background:#fff;\" target=\"_blank\"><span style=\"padding:0 0 0 40px;margin:0;display:block;overflow:hidden;background:transparent url(http://ads.gumgum.com/com/gumgum/tests/amazon.png) no-repeat scroll left center;\"><p style=\"padding:0;margin:0;font:bold 12px/16px Arial;color:#000;text-decoration:underline;display:block;height:16px;overflow:hidden;text-align:left;padding:2px 0;word-wrap:break-word;\">HBase Administration Cookbook</p><p style=\"padding:0;margin:0;font:italic 12px/16px Arial;color:#c60;text-decoration:none;display:block;height:16px;overflow:hidden;text-align:left;word-wrap:break-word;\">www.amazon.com</p></span><span style=\"position:absolute;bottom:0;right:70px;overflow:hidden;\"><img src=\"http://ecx.images-amazon.com/images/I/41OEZDHmUoL._SL75_.jpg\" style=\"border:none;max-width:64px;*width:64px;height:64px;\"></span></a></div></div>";


       webView.loadDataWithBaseURL("file:///android_asset/", customHtml , "text/html", "utf-8", null);

       button = (Button) findViewById(R.id.btnChangeImage);

    }

    public void sendMessage(View view) {

           webView.setWillNotCacheDrawing(false);
           webView.destroyDrawingCache();
           webView.setDrawingCacheEnabled(true);
           webView.measure(MeasureSpec.makeMeasureSpec(480, MeasureSpec.EXACTLY), 
                    MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY));
           webView.layout(0, 0,  webView.getMeasuredWidth(),  webView.getMeasuredHeight());


            webView.buildDrawingCache(true);
        Bitmap bmap = Bitmap.createBitmap(webView.getDrawingCache());
        webView.destroyDrawingCache();
        //imageView.setMaxHeight(55);
        //imageView.setMaxWidth(20);
        Bitmap bmap_New = scaleDownBitmap(bmap,400,this);
        Intent intent = new Intent(this, ImageViewActivity.class);
        intent.putExtra("BitmapImage", bmap_New);
        startActivity(intent);
    }

     public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {

         final float densityMultiplier = context.getResources().getDisplayMetrics().density;        

         int h= (int) (newHeight*densityMultiplier);
         int w= (int) (h * photo.getWidth()/((double) photo.getHeight()));

         photo=Bitmap.createScaledBitmap(photo, w, h, true);

         return photo;
         }

ImageViewActivity.java

    public class ImageViewActivity extends Activity {

    ImageView imageView;

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

        imageView = (ImageView) findViewById(R.id.imageView1);
        Intent intent = getIntent();
        Bitmap bmp = (Bitmap)intent.getParcelableExtra("BitmapImage");
        imageView.setImageBitmap(bmp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_image_view, menu);
        return true;
    }

}

我不知道它是否已链接,但是当我启动我的应用程序时,我的 catlog 中有这个:

E/chromium(32671): external/chromium/net/disk_cache/stat_hub.cc:190: [1214/103321:ERROR:stat_hub.cc(190)] StatHub::Init - App com.example.androidtestadserver isn't supported.
4

1 回答 1

0

我强烈推荐一种不同的方法。
通过意图传递整个位图需要大量内存,并且不能在旧手机上运行,​​比如说 SDK 2.3。
尝试将位图作为 byteArray 传递并构建它以在下一个活动中显示。如果你想在活动之间传递它,我会将它存储在一个文件中。这样效率更高,工作量也更少。您可以使用 MODE_PRIVATE 在数据文件夹中创建任何其他应用程序都无法访问的私有文件。
顺便说一句,以下对我有用:

public void sendMessage(View view) {

       webView.setWillNotCacheDrawing(false);
       webView.destroyDrawingCache();
       webView.setDrawingCacheEnabled(true);
       webView.measure(MeasureSpec.makeMeasureSpec(480, MeasureSpec.EXACTLY), 
                MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY));
       webView.layout(0, 0,  webView.getMeasuredWidth(),  webView.getMeasuredHeight());


        webView.buildDrawingCache(true);
    Bitmap bmap = Bitmap.createBitmap(webView.getDrawingCache());
    webView.destroyDrawingCache();
    //imageView.setMaxHeight(55);
    //imageView.setMaxWidth(20);
    Bitmap bmap_New = scaleDownBitmap(bmap,400,this);
    Intent intent = new Intent(this, ImageViewActivity.class);
    //Bitmap b; // your bitmap
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    bmap_New.compress(Bitmap.CompressFormat.PNG, 50, bs);
    intent.putExtra("BitmapImage", bs.toByteArray());
    //intent.putExtra("BitmapImage", bmap_New);
    startActivity(intent);
}

接收:

public class ImageViewActivity extends Activity {

    ImageView imageView;

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

        Log.d("ImageViewActivity", "Oncreate");

        imageView = (ImageView) findViewById(R.id.imageView1);
        //Intent intent = getIntent();
        //Bitmap bmp = (Bitmap)intent.getParcelableExtra("BitmapImage");
        if( getIntent().getExtras() != null ){
            Bitmap b = BitmapFactory.decodeByteArray(
                    getIntent().getByteArrayExtra("BitmapImage"),0,getIntent().getByteArrayExtra("BitmapImage").length);  
            imageView.setImageBitmap(b);

        }
        else{
            Log.d("ImageViewActivity", "null");
        }

    }


}
于 2012-12-14T01:59:31.463 回答