简体   繁体   English

在Android设备上显示所有图像

[英]Display all images on android device

I'm trying to create an application that on one of its activities it displays all the images on the device for the user to chose one of them. 我正在尝试创建一个应用程序,该应用程序在其一项活动中会在设备上显示所有图像,以供用户选择其中之一。

I managed to only display all the images on the DCIM/Camera folder. 我设法只显示DCIM / Camera文件夹中的所有图像。

This is my code for displaying the images: 这是我显示图像的代码:

GridView gridview = (GridView) findViewById(R.id.gridview);
imageAdapter = new ImageAdapter(this);
gridview.setAdapter(imageAdapter);
gridview.setBackgroundColor(getResources().getColor(R.color.Black));

String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();

String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";

Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);

File[] files = targetDirector.listFiles();
for (File file : files)
    imageAdapter.add(file.getAbsolutePath());

Can someone help me here? 有人可以帮我吗?

Thank you in advance! 先感谢您!

On click (maybe upload button) : 点击时(也许是上传按钮):

 Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

When user has selected an image : 用户选择图像后:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
            Uri selectedImage = data.getData();

            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inSampleSize = 8;
            final InputStream ist;
            try {
                ist = mActivity.getContentResolver().openInputStream(selectedImage);
                Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
                mPicturePath = compressImage(bitmap).getAbsolutePath();
                //recycling bitMap to overcome OutOfMemoryError
                if(bitmap!=null){
                    bitmap.recycle();
                    bitmap=null;
                }
                ist.close();

            //you have mPicturePath do something with it!
            } catch (FileNotFoundException e) {
                logoUploadFaied();
                e.printStackTrace();
            } catch (IOException e) {
                logoUploadFaied();
                e.printStackTrace();
            }
        } else {
           //Not uploading
        }
    }

Here is Compress Image code, it takes in a bitmap as argument, stores it locally (compressed) and returns file path. 这是Compress Image代码,它以位图作为参数,将其存储在本地(压缩)并返回文件路径。 Excellent if you want to upload your images to server like whatsapp: 如果要将图像上载到whatsapp等服务器上,效果很好:

 public File compressImage(Bitmap bmp) {
        Bitmap scaledBitmap = null;

        int actualHeight = bmp.getHeight();
        int actualWidth = bmp.getWidth();

//      max Height and width values of the compressed image is taken as 816x612
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = logoMaxWidth / logoMaxHeight;
//      width and height values are set maintaining the aspect ratio of the image
        if (actualHeight > logoMaxHeight || actualWidth > logoMaxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = logoMaxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) logoMaxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = logoMaxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) logoMaxWidth;
            } else {
                actualHeight = (int) logoMaxHeight;
                actualWidth = (int) logoMaxWidth;
            }
        }
        try {
            scaledBitmap = Bitmap.createScaledBitmap(bmp, actualWidth, actualHeight, true);
        } catch (OutOfMemoryError exception) {
            logoUploadFaied();
            exception.printStackTrace();
        }
        String uriSting = (System.currentTimeMillis() + ".jpg");
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + uriSting);
        try {
            file.createNewFile();
        } catch (IOException e) {
            logoUploadFaied();
            e.printStackTrace();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            logoUploadFaied();
            e.printStackTrace();
        }
        try {
            fos.write(getBytesFromBitmap(scaledBitmap));
            fos.close();
            //recycling bitMap to overcome OutOfMemoryError
            if(scaledBitmap!=null){
                scaledBitmap.recycle();
                scaledBitmap=null;
            }
        } catch (IOException e) {
            logoUploadFaied();
            e.printStackTrace();
        }
        return file;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM