我正在尝试显示来自SD卡HTC渴望Z Android Mobile的所有图像。我使用这个网站sample code:
但音乐视频都可以从SD卡访问。但我无法显示图像。没有错误,没有错误。只是显示黑屏。我如何访问图像?
任何人都可以有更好的主意。谁能给我一些示例代码?
顺便说一下,这个网站示例代码在另一个移动设备上工作正常。我测试一下
请提供一些积极的指导。
答案 0 :(得分:1)
你能做的是: -
Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{android.provider.MediaStore.Images.Media.DATA}, null, null, null);
if(cursor!=null && cursor.moveToFirst()) {
do {
//write your code here to show images in a view
// you can access data through cursor.getString(0)
} while(cursor.moveToNext());
}
希望有所帮助
答案 1 :(得分:1)
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"/>
<TextView android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/imageView"
android:layout_gravity="right|center_vertical"
android:text="Next"
android:textColor="@android:color/white"
android:textSize="30sp"/>
<TextView android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Prev"
android:textColor="@android:color/white"
android:textSize="30sp"/>
</FrameLayout>
这是java文件
import java.util.ArrayList; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Images.Media; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.ImageView; import android.widget.TextView; public class DisplayAllImages extends Activity { ImageView mImageView; TextView mNext; TextView mPrev; Cursor mCursor; ArrayList list; int mIndex = 0; int mSize; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); mImageView = (ImageView)findViewById(R.id.imageView); mNext = (TextView)findViewById(R.id.next); mPrev = (TextView)findViewById(R.id.prev); } @Override protected void onResume() { mCursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[] {Media.DATA}, null, null, null); if(mCursor!=null && mCursor.moveToFirst()) { list = new ArrayList(); mSize = mCursor.getCount(); do { list.add(mCursor.getString(0)); } while(mCursor.moveToNext()); mImageView.setImageURI(Uri.parse(list.get(mIndex))); } mNext.bringToFront(); mNext.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mIndex == (mSize-1)) { mIndex = -1; } mImageView.setImageURI(Uri.parse(list.get(++mIndex))); } }); mPrev.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mIndex == 0) { mIndex = mSize; } mImageView.setImageURI(Uri.parse(list.get(--mIndex))); } }); super.onResume(); } }