我该如何在安卓相册中展示来自特定文件夹的图片?

9

如何在Android相册中显示特定文件夹中的所有图片,例如Whatsapp所做的那样。我正在使用MediaScannerConnectionClient。

File folder = new File("/sdcard/myfolder/");
allFiles = folder.list();
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/myfolder/"+allFiles[0];
@Override
public void onScanCompleted(String path, Uri uri) {
    try {
        if (uri != null) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);

        }
    } finally {
        conn.disconnect();
        conn = null;
    }
}

private void startScan() {
    if (conn != null) {
        conn.disconnect();
    }
    conn = new MediaScannerConnection(this, this);
    conn.connect();
}
    @Override
public void onMediaScannerConnected() {
    conn.scanFile(SCAN_PATH, "image/*");
}

但是我在这一点上遇到了一个错误:
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    startActivity(intent);

具体在这里:
startActivity(intent);

无法获取以下内容的类型:content://media/external/images/media/267830 未找到可处理该意图的活动

在 onScanCompleted 中,我的路径和 uri 参数不为空。


当您提供静态路径时,无需调用您尝试使用意图的相册。 - Terril Thomas
你是在说我不需要 onScanCompleted 函数内的代码吗? - Rafael
是的,Action View 不会给你想要的静态路径。 - Terril Thomas
这个教程可以帮助你:http://startandroiddevelopment.blogspot.in/2013/10/importing-image-from-gallery.html - user834900
6个回答

13

嗨,你可以使用以下代码,希望能对你有所帮助。

package com.example.browsepicture;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BrowsePicture2 extends Activity {
    String SCAN_PATH;
    File[] allFiles ;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_picture);

        File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/aaaa/");
        allFiles = folder.listFiles();

        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        new SingleMediaScanner(BrowsePicture2.this, allFiles[0]);
                    }
                });
    }

    public class SingleMediaScanner implements MediaScannerConnectionClient {

        private MediaScannerConnection mMs;
        private File mFile;

        public SingleMediaScanner(Context context, File f) {
            mFile = f;
            mMs = new MediaScannerConnection(context, this);
            mMs.connect();
        }

        public void onMediaScannerConnected() {
            mMs.scanFile(mFile.getAbsolutePath(), null);
        }

        public void onScanCompleted(String path, Uri uri) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            startActivity(intent);
            mMs.disconnect();
        }

    }
}

talhakosen,我认为这个例子有点不同。除了开发自己的查看器之外,是否有一种方法可以使用Android本地图库来显示我的图像文件夹? - Rafael
1
但是不会 public void onScanCompleted(String path, Uri uri) 打开一个特定的文件吗?该文件是 allFiles[0] 吗? - Elad Benda
1
是的@EladBenda,它只是打开一个特定的文件。但它会在画廊中打开,以便用户可以通过滑动浏览图片。我同意这不是理想的解决方案。我正在基于AOSP开发一个应用内画廊库,目前我还没有找到类似的解决方案。 - georgiecasey
1
folder.listFiles(); 返回 null。我在那个文件夹中有2张图片。需要帮助吗? - Pranav Mahajan

3
你应该添加网格视图适配器类。
public class GalleryPictureActivity extends Activity
{
    private String[]        FilePathStrings;
    private File[]          listFile;
    GridView                grid;
    GridViewAdapter         adapter;
    File                    file;
    public static Bitmap    bmp = null;
    ImageView               imageview;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_picture);
        // Check for SD Card
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED))
        {
            Toast.makeText(this, "Error! No SDCARD Found!",
                    Toast.LENGTH_LONG).show();
        }
        else
        {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/images");
        }
        if (file.isDirectory())
        {
            listFile = file.listFiles();
            FilePathStrings = new String[listFile.length];
            for (int i = 0; i < listFile.length; i++)
            {
                FilePathStrings[i] = listFile[i].getAbsolutePath();
            }
        }
        grid = (GridView)findViewById(R.id.gridview);
        adapter = new GridViewAdapter(this, FilePathStrings);
        grid.setAdapter(adapter);

        grid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick (AdapterView<?> parent, View view,
                    int position, long id)
            {
                imageview = (ImageView)findViewById(R.id.imageView1);
                int targetWidth = 700;
                int targetHeight = 500;
                BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
                bmpOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(FilePathStrings[position],
                        bmpOptions);
                int currHeight = bmpOptions.outHeight;
                int currWidth = bmpOptions.outWidth;
                int sampleSize = 1;
                if (currHeight > targetHeight || currWidth > targetWidth)
                {
                    if (currWidth > currHeight)
                        sampleSize = Math.round((float)currHeight
                                / (float)targetHeight);
                    else
                        sampleSize = Math.round((float)currWidth
                                / (float)targetWidth);
                }
                bmpOptions.inSampleSize = sampleSize;
                bmpOptions.inJustDecodeBounds = false;
                bmp = BitmapFactory.decodeFile(FilePathStrings[position],
                        bmpOptions);
                imageview.setImageBitmap(bmp);
                imageview.setScaleType(ImageView.ScaleType.FIT_XY);
                bmp = null;

            }
        });

    }
}

另一个类 GridView 适配器:

   public class GridViewAdapter extends BaseAdapter
{
    private Activity                activity;
    private String[]                filepath;
    private static LayoutInflater   inflater    = null;
    Bitmap                          bmp         = null;

    public GridViewAdapter (Activity a, String[] fpath)
    {
        activity = a;
        filepath = fpath;
        inflater = (LayoutInflater)activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount ()
    {
        return filepath.length;
    }

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

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

    public View getView (int position, View convertView, ViewGroup parent)
    {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.gridview_item, null);
        ImageView image = (ImageView)vi.findViewById(R.id.image);
        int targetWidth = 100;
        int targetHeight = 100;
        BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
        bmpOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filepath[position], bmpOptions);
        int currHeight = bmpOptions.outHeight;
        int currWidth = bmpOptions.outWidth;
        int sampleSize = 1;
        if (currHeight > targetHeight || currWidth > targetWidth)
        {
            if (currWidth > currHeight)
                sampleSize = Math.round((float)currHeight
                        / (float)targetHeight);
            else
                sampleSize = Math.round((float)currWidth
                        / (float)targetWidth);
        }
        bmpOptions.inSampleSize = sampleSize;
        bmpOptions.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeFile(filepath[position], bmpOptions);
        image.setImageBitmap(bmp);
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        bmp = null;
        return vi;
    }
}

活动:

activity_gallery_picture:

图库图片活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

    <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" 
            android:layout_weight=".85">

   </GridView>

   <ImageView
           android:id="@+id/imageView1"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight=".25"
           android:scaleType="fitXY"
           android:src="@drawable/galleryimage" />

  </LinearLayout>

另一个活动布局:

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

Mediascanner在扫描时会显示已删除的图像。 - krishnan muthiah pillai

0

你可以使用android.database.Cursor

public boolean OpenGalleryFromFolder(android.content.Context context, String folderName)
{
    String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/Pictures/" + folderName + "/";
    return OpenGalleryFromPathToFolder(context, filePath);
}

// Finds the first image in the specified folder and uses it to open a the devices native gallery app with all images in that folder.
public boolean OpenGalleryFromPathToFolder(android.content.Context context, String folderPath)
{
    java.io.File folder = new java.io.File(folderPath);
    java.io.File[] allFiles = folder.listFiles();

    if (allFiles != null && allFiles.length > 0)
    {
        android.net.Uri imageInFolder = getImageContentUri(context, allFiles[0]);
        if (imageInFolder != null)
        {
            android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
            intent.setData(imageInFolder);
            context.startActivity(intent);
            return true;
        }
    }
    return false;
}

// converts the absolute path of a file to a content path
// absolute path example: /storage/emulated/0/Pictures/folderName/Image1.jpg
// content path example: content://media/external/images/media/47560
private android.net.Uri getImageContentUri(android.content.Context context, java.io.File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    android.database.Cursor cursor = context.getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[]{android.provider.MediaStore.Images.Media._ID},
            android.provider.MediaStore.Images.Media.DATA + "=? ",
            new String[]{filePath}, null);
    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.MediaColumns._ID));
        return android.net.Uri.withAppendedPath(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
    } else {
        if (imageFile.exists()) {
            android.content.ContentValues values = new android.content.ContentValues();
            values.put(android.provider.MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}

0

100% 工作

Kotlin 碎片

class MyPhotosFragment : Fragment() {
private var _binding : FragmentMyPhotosBinding? = null
val binding get() = _binding!!
private lateinit var myPhotoAdapter : MyPhotoAdapter
var file: File? = null
var filePath: ArrayList<String> = ArrayList()
var filename: ArrayList<String> = ArrayList()
private var listFile: Array<File>? = null
@SuppressLint("NotifyDataSetChanged")
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentMyPhotosBinding.inflate(layoutInflater)
    loadSpecificFile()
    myPhotoAdapter = MyPhotoAdapter(requireContext(), filePath, filename)
    binding.savedPhotoRecyclerView.layoutManager = GridLayoutManager(requireContext(),3)
    binding.savedPhotoRecyclerView.adapter = myPhotoAdapter
    myPhotoAdapter.notifyDataSetChanged()
    return binding.root
}

private fun loadSpecificFile() {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Toast.makeText(requireContext(), "Error! No SDCARD Found!", Toast.LENGTH_LONG).show()
    } else {

        file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path + "/PhotoEditor/")
        if (!file!!.exists()){
            file!!.mkdirs()
        }
        else{
                listFile = file!!.listFiles { _, name ->
                    name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(
                        ".png")
                }
                for (absolutePath in listFile!!) {
                    filePath.add(absolutePath.absolutePath)
                    filename.add(absolutePath.name)
                }
        }
    }
}

}

Kotlin 适配器
class MyPhotoAdapter(var context : Context, private var myPhotosPathList: ArrayList<String>, private var myPhotosNameList: ArrayList<String>) : RecyclerView.Adapter<MyPhotoAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context)
        .inflate(R.layout.saved_photo_layout, parent, false)

    return ViewHolder(
        view
    )
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    var file = File(myPhotosPathList[position]);
    Glide.with(context).load(file)
        .into(holder.savedPhoto)
    holder.savedPhoto.setOnLongClickListener {
        val builder = AlertDialog.Builder(context)
        builder.setMessage("Are you sure you want delete this?")
        builder.setPositiveButton("YES"
        ) { _, _ ->
            if (file.exists()) {
                file.delete()
            }
            myPhotosPathList.removeAt(position)
            notifyDataSetChanged()
        }
        builder.setNegativeButton("NO"
        ) { dialogInterface, i -> dialogInterface.cancel() }
        builder.show()
        false
    }
    holder.savedPhoto.setOnClickListener {
        val intent = Intent()
        intent.action = Intent.ACTION_VIEW
        val data: Uri = FileProvider.getUriForFile(
            context,
            BuildConfig.APPLICATION_ID + ".provider",
            File(myPhotosPathList[position])
        )
        intent.setDataAndType(data, "*/*")
        try {
            val substring: String =
                myPhotosNameList[position].substring(myPhotosNameList[position].lastIndexOf("."))
            if (substring == ".jpg" || substring == ".jpeg" || substring == ".png") {
                intent.setDataAndType(data, "image/*")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        try {
            context.startActivity(intent)
        } catch (unused: ActivityNotFoundException) {
            Toast.makeText(context, "No application available", Toast.LENGTH_SHORT).show()
        }
    }
}

override fun getItemCount(): Int {
    return myPhotosPathList.size
}

class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
    val savedPhoto : ImageView = itemView.findViewById(R.id.savedPhoto)

}

输出


0

@Talha给出的答案很好,但它尝试使用图像应用程序选项打开图像。如果您只想使用SD卡中的文件夹刷新图库,可以根据以下方式修改SingleMediaScanner的代码。

class SingleMediaScanner implements MediaScannerConnectionClient {

    private MediaScannerConnection mMs;
    private File mFile;

    public SingleMediaScanner(Context context, File f) {
        mFile = f;
        mMs = new MediaScannerConnection(context, this);
        mMs.connect();
    }

    public void onMediaScannerConnected() {
        mMs.scanFile(mFile.getAbsolutePath(), null);
    }

    public void onScanCompleted(String path, Uri uri) {

        mMs.disconnect();
    }

}

并且在按钮点击事件中循环遍历从每个文件获取的内容:

    File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/aaaa/");
    allFiles = folder.listFiles();

然后将其逐个传递给SingleMediaScanner。

在我的情况下,它起作用了。


0

你可以使用我的方法从SD卡上的特定文件夹获取图像,还可以删除文件。

MainActivity

    FileAdapter fileAdapter;
    RecyclerView recyclerView;
    ArrayList<String> filePath = new ArrayList<>();
    ArrayList<String> filename = new ArrayList<>();
    private File[] listFile;
    File file;


    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    tv_empty =  findViewById(R.id.tv_empty);

    loadFiles();
    fileAdapter = new FileAdapter(this, filePath, filename);

    recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
    recyclerView.setAdapter(fileAdapter);
    fileAdapter.notifyDataSetChanged();


    private void loadFiles() {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
        } else {
            file = new File(Environment.getExternalStorageDirectory().getPath() + "/StorySaver");
            if (!file.exists()) {
                file.mkdirs();
            }
        }

        filePath.clear();
        filename.clear();
        if (file.isDirectory()) {
            listFile = file.listFiles();
            for (File absolutePath : listFile) {
                filePath.add(absolutePath.getAbsolutePath());
                filename.add(absolutePath.getName());
            }
        }

        if (filePath.size() == 0) {
            tv_empty.setVisibility(View.VISIBLE);
        } else {
            tv_empty.setVisibility(View.GONE);
        }
    }

我的适配器

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.CustomViewHolder> {
        ArrayList<String> filepath;
        ArrayList<String> filename;
        public Context mContext;
        File file;

        public class CustomViewHolder extends RecyclerView.ViewHolder {
            TextView content;
            ImageView imageView;

            public CustomViewHolder(final View view) {
                super(view);
                this.content = (TextView) view.findViewById(R.id.content);
                this.imageView = (ImageView) view.findViewById(R.id.image);
            }
        }

        public FileAdapter(Context context, ArrayList<String> filepath, ArrayList<String> filename) {
            this.filepath = filepath;
            this.filename = filename;
            this.mContext = context;
        }


        public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            return new CustomViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_file, null));
        }

        public void onBindViewHolder(CustomViewHolder customViewHolder, final int i) {
            file = new File(filepath.get(i));
            customViewHolder.content.setText(filename.get(i));
            Glide.with(this.mContext).load(filepath.get(i)).into(customViewHolder.imageView);
            customViewHolder.imageView.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View view) {
                    Builder builder = new Builder(mContext);
                    builder.setMessage("Are you sure you want delete this?");
                    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int v) {
                            if (file.exists()) {
                                file.delete();
                            }
                            filepath.remove(i);
                            notifyDataSetChanged();
                        }
                    });
                    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });
                    builder.show();
                    return false;
                }
            });

            customViewHolder.imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    Uri data = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(filepath.get(i)));
                    intent.setDataAndType(data, "*/*");
                    try {
                        String substring = filename.get(i).substring(filename.get(i).lastIndexOf("."));

                        if (substring.equals(".jpg")) {
                            intent.setDataAndType(data, "image/*");
                        } else if (substring.equals(".mp4")) {
                            intent.setDataAndType(data, "video/*");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    try {
                        mContext.startActivity(intent);
                    } catch (ActivityNotFoundException unused) {
                        Toast.makeText(mContext, "No application available", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }

        public int getItemCount() {
            return filepath.size();
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接