android中文本文件的视图类似于android中图像的ImageView
我想做一个应用程序,它在网格视图中显示文档、pdf、txt等文本文件,就像ImageView显示图像一样。android中是否有这些文件的视图。 下面给出了代码,在这里我可以查看网格中的图像,但不能查看文档文件android中文本文件的视图类似于android中图像的ImageView,android,text-files,android-imageview,Android,Text Files,Android Imageview,我想做一个应用程序,它在网格视图中显示文档、pdf、txt等文本文件,就像ImageView显示图像一样。android中是否有这些文件的视图。 下面给出了代码,在这里我可以查看网格中的图像,但不能查看文档文件 public class MainActivity extends Activity implements OnItemClickListener{ public class ImageAdapter extends BaseAdapter { private Context
public class MainActivity extends Activity implements OnItemClickListener{
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path){
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(3, 3, 3, 3);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 100, 100);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
ImageAdapter myImageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
gridview.setOnItemClickListener(this);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/img/";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
public类MainActivity扩展活动实现了mclickListener{
公共类ImageAdapter扩展了BaseAdapter{
私有上下文;
ArrayList itemList=新建ArrayList();
公共图像适配器(上下文c){
mContext=c;
}
无效添加(字符串路径){
itemList.add(路径);
}
@凌驾
public int getCount(){
返回itemList.size();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
图像视图图像视图;
如果(convertView==null){//如果它没有被回收,初始化一些属性
imageView=新的imageView(mContext);
setLayoutParams(新的GridView.LayoutParams(70,70));
imageView.setScaleType(imageView.ScaleType.CENTER\U裁剪);
设置填充(3,3,3,3);
}否则{
imageView=(imageView)convertView;
}
位图bm=decodeSampledBitmapFromUri(itemList.get(position),100100);
设置图像位图(bm);
返回图像视图;
}
公共位图解码SampleDbitMapFromUri(字符串路径、int-reqWidth、int-reqHeight){
位图bm=null;
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(路径、选项);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
bm=BitmapFactory.decodeFile(路径、选项);
返回bm;
}
公共整数计算示例大小(
BitmapFactory.Options选项、int reqWidth、int reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
如果(宽度>高度){
inSampleSize=数学圆((浮动)高度/(浮动)要求高度);
}否则{
inSampleSize=数学圆((浮动)宽度/(浮动)宽度);
}
}
返回样本大小;
}
}
ImageAdapter myImageAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView GridView=(GridView)findViewById(R.id.GridView);
myImageAdapter=新的ImageAdapter(此);
setAdapter(myImageAdapter);
setOnItemClickListener(this);
字符串ExternalStorageDirectoryPath=环境
.getExternalStorageDirectory()
.getAbsolutePath();
字符串targetPath=ExternalStorageDirectoryPath+“/img/”;
Toast.makeText(getApplicationContext(),targetPath,Toast.LENGTH_LONG).show();
文件targetDirector=新文件(targetPath);
File[]files=targetDirector.listFiles();
用于(文件:文件){
添加(file.getAbsolutePath());
}
}
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
}
}
像这样使用:
if(filetype.toString().equals(".pdf"))
{
//place your pdf image
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
}
else if(filetype.toString().equals(".docs"))
{
//place your docs image
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
}
else if(filetype.toString().equals(".txt "))
{
//place your txt image
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
}
。希望这能帮助你if(filetype==pdf){setPdfmage}else if(filetype==txt){setTxtmage}else if…@MocialovBoris非常感谢你在@MocialovBoris comment if(filetype.toString().equals)(“pdf”){setpdfimage}中做一些小改动,如果(filetype.toString().equals)(“txt”){settxtmages}哦,它奏效了,谢谢@Nirmal@SangeetaRawat-这解决了你的问题吗?你得到输出了吗?