如何创建动态文件视图?
您好,我希望创建动态文件视图图库图像,但我不知道如何创建动态文件视图......请帮助我......给我你的建议......
这是我的画廊视图当前代码:
package ImageViewExample.ImageViewExample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
//import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
Gallery imagegrid;
private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (Gallery) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(),
ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new Gallery.LayoutParams(110,110));
}
else {
i = (ImageView) convertView;
}
return i;
}
}
}
这是我的 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">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:spacing="16dp"
/>
</LinearLayout>
log cat 错误:
04-25 23:06:14.329: INFO/Process(391): Sending signal. PID: 391 SIG: 9
04-25 23:06:14.389: INFO/ActivityManager(63): Process
ImageViewExample.ImageViewExample (pid 391) has died.
04-25 23:06:14.800: WARN/InputManagerService(63): Window already focused, ignoring
focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43fdc730
04-25 23:06:26.009: DEBUG/AndroidRuntime(403): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:26.019: DEBUG/AndroidRuntime(403): CheckJNI is ON
04-25 23:06:26.580: DEBUG/AndroidRuntime(403): --- registering native functions ---
04-25 23:06:28.259: DEBUG/AndroidRuntime(403): Shutting down VM
04-25 23:06:28.300: DEBUG/dalvikvm(403): Debugger has detached; object registry had 1
entries
04-25 23:06:28.329: INFO/AndroidRuntime(403): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): CheckJNI is ON
04-25 23:06:29.839: DEBUG/AndroidRuntime(411): --- registering native functions ---
04-25 23:06:31.359: INFO/ActivityManager(63): Starting activity: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-25 23:06:31.610: DEBUG/AndroidRuntime(411): Shutting down VM
04-25 23:06:31.730: DEBUG/dalvikvm(411): Debugger has detached; object registry had 1
entries
04-25 23:06:31.786: INFO/AndroidRuntime(411): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:32.019: INFO/ActivityManager(63): Start proc
ImageViewExample.ImageViewExample for activity
ImageViewExample.ImageViewExample/.ImageViewExample: pid=418 uid=10050 gids={}
04-25 23:06:33.169: DEBUG/AndroidRuntime(418): Shutting down VM
04-25 23:06:33.169: WARN/dalvikvm(418): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): FATAL EXCEPTION: main
04-25 23:06:33.229: ERROR/AndroidRuntime(418): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.ImageViewExample
.ImageViewExample}: java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Looper.loop(Looper.java:123)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invoke(Method.java:521)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
dalvik.system.NativeStart.main(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): Caused by:
java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.MyImageView.<init>(MyImageView.java:32)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.ImageViewExample.onCreate(ImageViewExample.java:28)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): ... 11 more
04-25 23:06:33.279: WARN/ActivityManager(63): Force finishing activity
ImageViewExample.ImageViewExample/.ImageViewExample
04-25 23:06:33.839: WARN/ActivityManager(63): Activity pause timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:06:45.281: WARN/ActivityManager(63): Activity destroy timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:10:06.659: DEBUG/SntpClient(63): request time failed:
java.net.SocketException:
Address family not supported by protocol
Hi I wish to create dynamic file view gallery images but I have no idea how to create dynamic file view.....please help me......give me your suggestion.....
This is for my gallery view present code:
package ImageViewExample.ImageViewExample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
//import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
Gallery imagegrid;
private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (Gallery) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(),
ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new Gallery.LayoutParams(110,110));
}
else {
i = (ImageView) convertView;
}
return i;
}
}
}
This is my Xml coding:
<?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">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:spacing="16dp"
/>
</LinearLayout>
log cat error:
04-25 23:06:14.329: INFO/Process(391): Sending signal. PID: 391 SIG: 9
04-25 23:06:14.389: INFO/ActivityManager(63): Process
ImageViewExample.ImageViewExample (pid 391) has died.
04-25 23:06:14.800: WARN/InputManagerService(63): Window already focused, ignoring
focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43fdc730
04-25 23:06:26.009: DEBUG/AndroidRuntime(403): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:26.019: DEBUG/AndroidRuntime(403): CheckJNI is ON
04-25 23:06:26.580: DEBUG/AndroidRuntime(403): --- registering native functions ---
04-25 23:06:28.259: DEBUG/AndroidRuntime(403): Shutting down VM
04-25 23:06:28.300: DEBUG/dalvikvm(403): Debugger has detached; object registry had 1
entries
04-25 23:06:28.329: INFO/AndroidRuntime(403): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): CheckJNI is ON
04-25 23:06:29.839: DEBUG/AndroidRuntime(411): --- registering native functions ---
04-25 23:06:31.359: INFO/ActivityManager(63): Starting activity: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-25 23:06:31.610: DEBUG/AndroidRuntime(411): Shutting down VM
04-25 23:06:31.730: DEBUG/dalvikvm(411): Debugger has detached; object registry had 1
entries
04-25 23:06:31.786: INFO/AndroidRuntime(411): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:32.019: INFO/ActivityManager(63): Start proc
ImageViewExample.ImageViewExample for activity
ImageViewExample.ImageViewExample/.ImageViewExample: pid=418 uid=10050 gids={}
04-25 23:06:33.169: DEBUG/AndroidRuntime(418): Shutting down VM
04-25 23:06:33.169: WARN/dalvikvm(418): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): FATAL EXCEPTION: main
04-25 23:06:33.229: ERROR/AndroidRuntime(418): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.ImageViewExample
.ImageViewExample}: java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Looper.loop(Looper.java:123)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invoke(Method.java:521)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
dalvik.system.NativeStart.main(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): Caused by:
java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.MyImageView.<init>(MyImageView.java:32)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.ImageViewExample.onCreate(ImageViewExample.java:28)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): ... 11 more
04-25 23:06:33.279: WARN/ActivityManager(63): Force finishing activity
ImageViewExample.ImageViewExample/.ImageViewExample
04-25 23:06:33.839: WARN/ActivityManager(63): Activity pause timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:06:45.281: WARN/ActivityManager(63): Activity destroy timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:10:06.659: DEBUG/SntpClient(63): request time failed:
java.net.SocketException:
Address family not supported by protocol
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在您的活动中调用它, setContentView(new MyImageView(this));
to call this in your activity, setContentView(new MyImageView(this));