如何显示保存在android中sdcard文件夹中的图像

如何显示保存在android中sdcard文件夹中的图像

本文介绍了如何显示保存在android中sdcard文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我正在显示保存在sdcard文件夹中的图像,我找到了以下链接。





我使用以下代码从sdcard中的指定文件夹获取图像,但这里我得到0计数。



MyCode.jav

  String [] projection = {MediaStore。 Images.Media._ID}; 

final String [] columns = {MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA +like?,
new String [] { / my_images},
null);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap [this.count];
this.arrPath = new String [this.count];
this.thumbnailsselection = new boolean [this.count];

for(int i = 0; i imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnailails [i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext()。getContentResolver(),id,
MediaStore.Images.Thumbnails.MICRO_KIND,null);
arrPath [i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
secure_gallery_grid.setAdapter(imageAdapter);
imagecursor.close();

但这里按照以下链接显示保存在sd卡中的所有图像。 但是在这里我想显示保存在特定文件夹中的图像,就像我创建的My_images文件夹& 我要显示该文件夹中的图片。需要在gridview中显示为每个链接。任何人都可以帮助我这个。



感谢在advacne ..

解决方案


一旦你得到文件的路径,你就可以在gridview中显示图像

  ArrayList< String> f = new ArrayList< String>(); //文件路径列表
File [] listFile;

public void getFromSdcard()
{
文件file = new File(android.os.Environment.getExternalStorageDirectory(),TMyFolder);

if(file.isDirectory())
{
listFile = file.listFiles();


for(int i = 0; i {

f.add(listFile [i]。 getAbsolutePath());

}
}
}

添加权限清单文件

 < uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/& 

默认情况下,写入权限具有读取权限。



示例



main.xml

  ?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parentandroid:layout_height =fill_parent>

< GridView
android:id =@ + id / PhoneImageGrid
android:layout_width =fill_parent
android:layout_height =fill_parent
android:columnWidth =90dp
android:gravity =center
android:horizo​​ntalSpacing =10dp
android:numColumns =auto_fit
android:stretchMode = columnWidth
android:verticalSpacing =10dp/>

< / RelativeLayout>

gelleryitem.xml

 <?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parentandroid:layout_height =fill_parent>
< ImageView android:id =@ + id / thumbImageandroid:layout_width =wrap_content
android:layout_height =wrap_contentandroid:layout_centerInParent =true/>
< CheckBox android:id =@ + id / itemCheckBoxandroid:layout_width =wrap_content
android:layout_height =wrap_contentandroid:layout_alignParentRight =true
android:layout_alignParentTop =true/>
< / RelativeLayout>

AndroidCustomGalleryActivity.java

  public class AndroidCustomGalleryActivity extends Activity {
private int count;
private Bitmap [] thumbnailails;
private boolean [] thumbnailailsselection;
private String [] arrPath;
private ImageAdapter imageAdapter;
ArrayList< String> f = new ArrayList< String>(); //文件路径列表
File [] listFile;


/ **在首次创建活动时调用。 * /
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getFromSdcard();
GridView imagegrid =(GridView)findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);


}
public void getFromSdcard()
{
文件文件=新文件(android.os.Environment.getExternalStorageDirectory(),MapleBear );

if(file.isDirectory())
{
listFile = file.listFiles();


for(int i = 0; i {

f.add(listFile [i]。 getAbsolutePath());

}
}
}

public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public ImageAdapter(){
mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount(){
return f.size();
}

public Object getItem(int position){
return position;
}

public long getItemId(int position){
return position;
}

public查看getView(int position,View convertView,ViewGroup parent){
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem,null);
holder.imageview =(ImageView)convertView.findViewById(R.id.thumbImage);

convertView.setTag(holder);
}
else {
holder =(ViewHolder)convertView.getTag();
}


位图myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}
class ViewHolder {
ImageView imageview;


}
}

/ p>

>


As i am working on displaying images saved in sdcard folder, I found the following link.

Displaying images in gridview from SDCard.

I am using the following code to get images from specified folder in sdcard,but here i am getting 0 count.

MyCode.jav

    String[] projection = {MediaStore.Images.Media._ID};

    final String[] columns = { MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    Cursor imagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            MediaStore.Images.Media.DATA + " like ? ",
            new String[] {"/my_images"},
            null);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];

    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
        arrPath[i] = imagecursor.getString(dataColumnIndex);
    }
    imageAdapter = new ImageAdapter();
    secure_gallery_grid.setAdapter(imageAdapter);
    imagecursor.close();

But here as per the following link all the images saved in sd card are displaying. But here i want to display images which are saved in particular folder, as like i created "My_images" folder & saved images in that folder. I want to display images from that folder & need to display in gridview as per link. Can anyone please help me with this.

Thanks in advacne..

解决方案

You can get the path of files from a particualr folder as below

Once you get the path of files you ca display the images in gridview

ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;

public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"TMyFolder");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                f.add(listFile[i].getAbsolutePath());

            }
        }
}

Remember to add permissionin manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

By having write permission will have read permission by default.

Example

main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

   </RelativeLayout>

gelleryitem.xml

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerInParent="true" />
<CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />
    </RelativeLayout>

AndroidCustomGalleryActivity.java

   public class AndroidCustomGalleryActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getFromSdcard();
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);


}
public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"MapleBear");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                f.add(listFile[i].getAbsolutePath());

            }
        }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;


}
    }

Snap shot

这篇关于如何显示保存在android中sdcard文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:52