我对android很新,我真的需要帮助。我正在尝试编写一些代码,使用GridView在SD卡上显示图片,但到目前为止,当我运行应用程序时,只显示顶部的textview。我想知道我的代码在主要活动代码,图像适配器类代码或两者中的逻辑是否存在严重缺陷。这是我的代码:
package com.newtestforsdcarddisplay;
import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.Toast;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;
import android.net.Uri;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
public Cursor myImageCursor;
public int columnNumber;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] imageIDs = new String[]{Thumbnails._ID};
Uri myImagesSource = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
myImageCursor = managedQuery(myImagesSource,
imageIDs, null, null, MediaStore.Images.Thumbnails._ID);
columnNumber = myImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
GridView PhoneImageView = (GridView)findViewById(R.id.sdcard);
PhoneImageView.setAdapter(new ImageAdapter(this));
PhoneImageView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String[] data = { MediaStore.Images.Media.DATA };
Cursor viewImageCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, data,
null, null, MediaStore.Images.Thumbnails._ID );
int imageColumnIndex = viewImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
viewImageCursor.moveToPosition(position);
viewImageCursor.moveToFirst();
String filepath = viewImageCursor.getString(imageColumnIndex);
Toast.makeText(MainActivity.this, filepath, Toast.LENGTH_LONG).show();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filepath);
}
});
}
}
package com.newtestforsdcarddisplay;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.net.Uri;
import android.provider.MediaStore;
public class ImageAdapter extends BaseAdapter{
final MainActivity pca = new MainActivity();
private Context context;
public ImageAdapter(Context localContext) {
// context = localContext;
}
public int getCount() {
// return pca.myImageCursor.getCount();
return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
pca.myImageCursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = pca.myImageCursor.getInt(pca.columnNumber);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
package com.newtestforsdcarddisplay;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.net.Uri;
import android.provider.MediaStore;
public class ImageAdapter extends BaseAdapter{
final MainActivity pca = new MainActivity();
private Context context;
public ImageAdapter(Context localContext) {
// context = localContext;
}
public int getCount() {
// return pca.myImageCursor.getCount();
return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
pca.myImageCursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = pca.myImageCursor.getInt(pca.columnNumber);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<GridView
android:id="@+id/sdcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
有人可以帮我吗????正如我之前所说的,我对android很新,而且我很长时间都在努力解决这个问题。任何帮助都会非常感激。
答案 0 :(得分:0)
这个问题你还需要帮助吗?
我要做的是像这样创建一个“根”布局
<?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">
<include layout="@layout/my_header"/>
<include layout="@layout/my_grid"/>
</LinearLayout>
在my_header.xml中,只需将文本视图设置为线性布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
然后在my_grid.xml中,设置gridview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<GridView
android:id="@+id/sdcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
祝你好运
答案 1 :(得分:0)
您的getCount()
返回0.我认为您应该返回myImageCursor.getCount()
您可以将构造函数更改为
ImageAdapter(Context ctx,Cursor cr)
{
this.context=ctx;
this.cursor=cr;
}
然后在适配器的cr.getCount()
getCount()