图片未显示在图库中

时间:2017-04-18 12:31:06

标签: java android image file gallery

我从图像视图中获取图像并将其转换为位图,之后我将其保存在目录中。 图像已成功保存在我的目录中,但未在厨房中显示。

我在相机目录中有一些其他图像,可以看到从相机拍摄照片后保存的图像。我只是从ImageView保存图像并将其存储到目录中,在下一个屏幕中我只想使用保存的图像来获取信息。

textToImage()方法用于创建编码图像。

     Bitmap TextToImageEncode(String Value) throws WriterException {
                        BitMatrix bitMatrix;
                        try {
                            bitMatrix = new MultiFormatWriter().encode(
                                    Value,
                                    BarcodeFormat.DATA_MATRIX.QR_CODE,
                                    QRcodeWidth, QRcodeWidth, null
                            );

                        } catch (IllegalArgumentException Illegalargumentexception) {

                            return null;
                        }
                        int bitMatrixWidth = bitMatrix.getWidth();

                        int bitMatrixHeight = bitMatrix.getHeight();

                        int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

                        for (int y = 0; y < bitMatrixHeight; y++) {
                            int offset = y * bitMatrixWidth;

                            for (int x = 0; x < bitMatrixWidth; x++) {

                                pixels[offset + x] = bitMatrix.get(x, y) ?
                                        getResources().getColor(R.color.QRCodeBlackColor) : getResources().getColor(R.color.QRCodeWhiteColor);
                            }
                        }
                        Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

                        bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);

                        if (bitmap != null) 
                           saveImageToSdCard(bitmap);
                        } else {
                            Log.e("Test data image or not", "No bitmap given");
                        }

                        return bitmap;
                    }  

“我已使用此方法将图像保存在我的目录中”

public void saveImageToSdCard(Bitmap bitmap1) {
                    File sdCard = Environment.getExternalStorageDirectory();

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

                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                            Locale.getDefault()).format(new Date());



                    if (sdCard != null) {

> Create the directory here with path 

                        File directory = new File(ExternalStorageDirectoryPath+"/My App");
                        if (directory.exists()) {
                            directory.delete();
                            Log.e("it run", "directory is exists");
                        }
                        directory.mkdirs();
                        String strImageName = File.separator + "IMG_" + timeStamp + ".jpg";
                        File file = new File(directory, strImageName);
                        // strFinalImage = file.getAbsolutePath();
                        // Log.e(TAG + "strFinalImage22", strFinalImage + "");

> here we just scan the media 
                        MediaScannerConnection.scanFile(getActivity(),
                                new String[] { file.toString() }, null,

   new MediaScannerConnection.OnScanCompletedListener() {
                                    public void onScanCompleted(String path, Uri uri) {
                                        Log.e("ExternalStorage", "Scanned " + path + ":");
                                        Log.e("ExternalStorage", "-> uri=" + uri);
                                    }
                                });
                        FileOutputStream fileOutputStream = null;
                        try {
                            fileOutputStream = new FileOutputStream(file);
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            bitmap1.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
                            fileOutputStream.write(bytes.toByteArray());
                            fileOutputStream.flush();
                            fileOutputStream.close();
                            // Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();

                        } catch (FileNotFoundException e) {
                        } catch (IOException e) {
                        }

                    } else {
                        Toast.makeText(getActivity(), "Sd card mount", Toast.LENGTH_SHORT).show();
                    }
                }

0 个答案:

没有答案