2013-07-27 66 views
0

我一直在为此工作了几天。我想要做的就是将存储在SD卡上的图像放入片段内的ImageView中。ImageView未显示在片段中

我用这个教程的抽屉式导航栏布局: http://developer.android.com/training/implementing-navigation/nav-drawer.html

而这一次为更深入地执行: hxxp://www.androidbegin.com/tutorial/implementing-actionbarsherlock-side-menu-navigation -drawer功能于机器人/

这是fragment1.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/workingimageview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true"/> 
<!-- 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/Fragment1" 
    android:id="@+id/textview1" /> 
--> 
</RelativeLayout> 

这是包含内容和抽屉导航的XML:

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<!-- The main content view --> 
<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- Slideout Menü --> 
<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:background="#111" /> 

</android.support.v4.widget.DrawerLayout> 

这是onCreateView源代码中的片段:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
    savedInstanceState) 
{ 
    View rootView = inflater.inflate(R.layout.fragment1, container, false); 

    /** Load temporary image */ 
    /** Check if folder exists first */ 

    if(checkForWorkingDirectory()) 
    { 
     workingImageView = (ImageView) 
        rootView.findViewById(R.id.workingimageview); 
      workingImageView.setImageBitmap(scaleImage.decodeSampledBitmapFromResource(returnAbsoluteFilePath(),100,100)); 
    } 


    return rootView; 

    //return inflater.inflate(R.layout.fragment1, null); 
} 

checkForWorkingDirectory()基本上查找该图像位于 和returnAbsoluteFilePath()的目录是不言而喻的:)

的ImageView完全不显示。但是,如果我将ImageView注释掉,并且只显示TextView,则文本显示得很好。

我也试过这个,因为当我加载图像我不那时知道的ImageView的尺寸: http://adanware.blogspot.de/2012/06/android-getting-measuring-fragment.html

这些教程立足我用什么来显示里面的一个ImageView的最好的和正确的方法分段?

如果您需要更多的代码或信息,请告诉我。 在此先感谢!

+0

你是如何为ImageView的设置 “SRC”?你可以分享该代码吗? –

+0

编辑;;)值“100”,“100”只是一个例子。他们应该以某种方式读取,以适应包含片段的整个可用大小。 – cgew85

回答

-4

错误是别的什么地方我从来没有猜到。因为解决方案没有关系,所以没有必要张贴在这里。

+1

有同样的问题,很高兴看到你的解决方案是否会修复我的,即使不相关。 – mmaitlen

0

在我的代码中看到同样的问题后,我发现我的问题后,实际上看我的日志,我发现以下错误。

位图太大而无法上传到纹理(2340x4160,最大= 4096x4096)。

我通过扩展ImageView类和重载setImageBitmap和onMeasure方法解决了我的问题。

公共类ScaledImageView扩展的ImageView {

private Bitmap mBitmap; 

public ScaledImageView(Context context) { 
    this(context, null); 
} 

public ScaledImageView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public ScaledImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
public void setImageBitmap(Bitmap bm) { 
    mBitmap = bm; 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 

    if (mBitmap != null) { 
     int w = mBitmap.getWidth(); 
     int h = mBitmap.getHeight(); 
     int scaledHeight = (int)(((float)parentWidth/(float)w) * h); 
     super.setImageBitmap(Bitmap.createScaledBitmap(mBitmap, parentWidth, scaledHeight, true)); 
    } 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

}