在android中列出捕获图像的图像Url&#s

时间:2017-08-26 08:49:45

标签: android

  1. 我正在捕捉图像并存储它
  2. 图像以指定路径创建
  3. 但我无法通过光标
  4. 显示图像

    如何解决此问题

    捕捉它后如何存储图像::

    private void createFile(byte[] fileData) throws IOException {
    
            FileOutputStream out=null;
            try {
    
                //Create the directory
                String mDirectoryPath = Environment.DIRECTORY_PICTURES + LinksAndKeys.DIRECTORY_PATH_FOR_IMAGES;
                String mImageName = System.currentTimeMillis()+".jpg";
                File root = Environment.getExternalStoragePublicDirectory(mDirectoryPath);
                File dir = new File(root + File.separator);
                if (!dir.exists()) dir.mkdir();
    
                //Create file..
                File file = new File(root + File.separator + mImageName);
                file.createNewFile();
    
                out = new FileOutputStream(file);
                if(out!=null){
                    out.write(fileData);
                }
    
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                out.close();
            }
    
        }
    

    我如何尝试获取已创建图像的URI ::

     private ArrayList<String> loadPhotosFromNativeGallery() {
    
    
            final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    
            Cursor imagecursorExternalUri = getActivity().managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy + " DESC");
    
            Cursor imagecursorInternalUri = getActivity().managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy + " DESC");
    
            Cursor[] cursorArray = { imagecursorExternalUri, imagecursorInternalUri};
    
            MergeCursor mMergeCursor = new MergeCursor(cursorArray);
    
    
            ArrayList<String> imageUrls = new ArrayList<String>();
    
            for (int i = 0; i < mMergeCursor.getCount(); i++) {
                mMergeCursor.moveToPosition(i);
                int dataColumnIndex = mMergeCursor.getColumnIndex(MediaStore.Images.Media.DATA);
                imageUrls.add(mMergeCursor.getString(dataColumnIndex));
    
                System.out.println("=====> Array path => "+imageUrls.get(i));
            }
    
    
            return imageUrls;
        }
    

1 个答案:

答案 0 :(得分:0)

系统会在安装SD卡时扫描SD卡以查找任何新图像(和其他)文件。如果以编程方式添加文件,则必须扫描新文件。你可以使用以下方法来做到这一点:

MediaScannerConnection.scanFile(this, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);

您可以从this link

找到更多信息

您可以直接插入数据:

  ContentValues values = new ContentValues();
  values.put(MediaStore.Images.Media.TITLE, "YourTitle");
  values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
  values.put(MediaStore.MediaColumns.DATA, filePath);

  context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);