Android基本编码尝试抓住显示吐司无法正常工作?

时间:2012-05-20 19:38:44

标签: android image debugging error-handling try-catch

我正在构建一个真正简单的Android应用程序,但很多都出错了。

我甚至无法获得一个体面的尝试抓住一切正在运行。而且由于我得到的机器功能非常不足,我正在我的Android设备上直接测试,但这并没有真正起作用。

即使使用通用try catch,应用程序似乎在执行基本操作后崩溃。我在这做错了什么。

为什么尝试抓不到任何东西?

这是我的Main.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"
    >

 <ImageButton
        android:id="@+id/ib"
        android:layout_width="300dip"
        android:layout_height="50dip"
        android:text="Hello World, MyActivity"
        android:scaleType="fitXY"
        android:onClick="onButtonClicked"
        />

这是我的简单代码(图片甚至没有显示)

public void onButtonClicked(View v) {
    try {
        String strFilePath = "/mnt/sdcard/somefile.jpg";
        if (strFilePath.length() > 0) {

            File file = new File(strFilePath);

            if (file.exists()) {
                ShowToast(strFilePath + "Bestaat");

                ImageButton image = (ImageButton) findViewById(R.id.ib);
                Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
                image.setImageBitmap(bMap);

                ShowToast( "Done");
            } else {
                ShowToast(strFilePath + "Bestaat NIET");
            }
        } else {
            ShowToast(strFilePath + "strFilePath.length() =0");
        }

    } catch (Exception e) {
        HandleError(e);
    }
}

解决方案

大小超过3Mb的文件。 ImageButton不会显示“大”的图片,也不会显示任何内容。 该应用程序可能会抛出一些随机的内存异常加载整个应用程序的图像。

读取logcat足以调试这个'问题'

一些有用的代码(从其他SO问题中借鉴)

    ImageButton image = (ImageButton) findViewById(R.id.ib);

    Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
    Bitmap bSized = getResizedBitmap(bMap,150,150);
    image.setImageBitmap(bSized);

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)                     
    {
      int width = bm.getWidth();
      int height = bm.getHeight();
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;

      // create a matrix for the manipulation
      Matrix matrix = new Matrix();

      // resize the bit map
      matrix.postScale(scaleWidth, scaleHeight);

      // recreate the new Bitmap
      Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
      return resizedBitmap;

}

2 个答案:

答案 0 :(得分:1)

要么您没有带有该ID的ImageButton,要么您尝试加载的图片要大,以适应缩减的应用程序内存。

答案 1 :(得分:1)

我已经测试了您的代码段。

首先,ImageButton不会显示android:text部分,因为该属性属于Button ..

只需使用res / layout文件夹中main.xml中的LinearLayout和ImageButton,这段代码就可以了:

public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void onButtonClicked(View v) {
    try {
        String strFilePath = "/mnt/sdcard/somefile.png";
        if (strFilePath.length() > 0) {

            File file = new File(strFilePath);

            if (file.exists()) {
                //ShowToast(strFilePath + "Bestaat");

                ImageButton image = (ImageButton) findViewById(R.id.ib);
                Bitmap bMap = BitmapFactory.decodeFile(strFilePath);
                image.setImageBitmap(bMap);

                //ShowToast( "Done");
            } else {
                //ShowToast(strFilePath + "Bestaat NIET");
            }
        } else {
            //ShowToast(strFilePath + "strFilePath.length() =0");
        }

    } catch (Exception e) {
        //HandleError(e);
        e.printStackTrace();
    }
}
}

因此,无论是K-Ballo建议的文件还是大文件,或者您的ShowToast或HandleError方法都有问题。