Android ImageView可以';加载图像(jpg、png等)

Android ImageView可以';加载图像(jpg、png等),android,image,bitmap,imageview,Android,Image,Bitmap,Imageview,我正在尝试从我的res/drawable文件夹加载图像。我使用了Android开发者的指南。 出于某种原因,它无法工作。我得到的唯一错误是“SPAN_EXCLUSIVE\u EXCLUSIVE SPAN不能有零长度”,我对此进行了研究。显然这与自定义键盘有关,但我根本没有使用文本输入。应用程序本身没有崩溃。我希望你们能帮助我:) 布局文件仅包含带有ImageView的RelativeLayout public class PixelActivity extends Activity { @Ov

我正在尝试从我的res/drawable文件夹加载图像。我使用了Android开发者的指南。 出于某种原因,它无法工作。我得到的唯一错误是“SPAN_EXCLUSIVE\u EXCLUSIVE SPAN不能有零长度”,我对此进行了研究。显然这与自定义键盘有关,但我根本没有使用文本输入。应用程序本身没有崩溃。我希望你们能帮助我:) 布局文件仅包含带有ImageView的RelativeLayout

public class PixelActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pixel);
    String uri = "@drawable-hdpi/testbild.png";
    final int imageResource = getResources().getIdentifier(uri, null, getPackageName());


    final ImageView iv = (ImageView) findViewById(R.id.imageview1);

    //int imageHeight = options.outHeight;
    //int imageWidth = options.outWidth;
    //String imageType = options.outMimeType;
    new Thread(new Runnable()
    {

        public void run()
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getResources(), imageResource, options);


            iv.post(new Runnable()
            {

                public void run()
                {
                    iv.setImageBitmap(decodeSampledBitmapFromResources(getResources(),imageResource,iv.getWidth(),iv.getHeight()));
                    //iv.setImageResource(R.drawable.testbild);
                }
            });
        }
    });






}

public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight)
{
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if(height > reqHeight || width > reqWidth)
    {
        final int heightRatio = Math.round((float)height/(float)reqHeight);
        final int widthRatio = Math.round((float)width/(float)reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResources(Resources res, int resId, int reqWidth, int reqHeight)
{
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeResource(res, resId, options);
}
公共类像素活动扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_像素);
字符串uri=“@drawable hdpi/testbild.png”;
final int-imageResource=getResources().getIdentifier(uri,null,getPackageName());
最终ImageView iv=(ImageView)findViewById(R.id.imageview1);
//int imageHeight=options.outHeight;
//int imageWidth=options.outWidth;
//字符串imageType=options.outMimeType;
新线程(newrunnable())
{
公开募捐
{
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(getResources(),imageResource,选项);
四.员额(新的可运行()
{
公开募捐
{
iv.setImageBitmap(decodeSampledBitmapFromResources(getResources(),imageResource,iv.getWidth(),iv.getHeight());
//iv.setImageResource(R.drawable.testbild);
}
});
}
});
}
公共静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight)
{
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度)
{
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
公共静态位图decodeSampledBitmapFromResources(资源res、int resId、int reqWidth、int reqHeight)
{
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(res、resId、options);
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeResource(res、resId、options);
}
}

这是无效的。删除
-hdpi
部分和
.png
部分,然后重试。或者,切换到向
getIdentifier()
提供所有三个参数:


尝试runOnUiThread而不是view.post。View.post有时根本不会被调用。只有Handler.post和Activity.runOnUiThread始终适用于我。
String uri = "@drawable-hdpi/testbild.png";
final int imageResource = getResources().getIdentifier("testbild", "drawable", getPackageName());