0

我目前正在开发一个通过套接字接收多个图像的应用程序。为了保存它们,我编写了以下方法:

public static boolean saveTempImageToGallery(Context c) {
    try {
        FileInputStream fis = c.openFileInput(Settings.TEMP_PHOTO_STORAGE);
        // create name of file: [date]-[time]-baby
        final String tFilename = new SimpleDateFormat("dd-MM-yyyy_hh-mm-ss")
                .format(new Date()) + ".png";

        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            Log.d(TAG, "External storage available.");
            // sd card available
            File dir = getExternalStorageDir("Photos");
            if (dir.mkdirs() || dir.isDirectory()) {
                Log.i(TAG, "Directory: "+dir.getAbsolutePath());
                File newImage = new File(dir, tFilename);
                if (newImage.createNewFile() && newImage.isFile()) {
                    Log.i(TAG, "Saving image to "+newImage.getAbsolutePath());
                    final Bitmap bmp = BitmapFactory.decodeStream(fis);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // compress image to png
                    bmp.compress(Bitmap.CompressFormat.PNG, 40, baos);
                    FileOutputStream fo = new FileOutputStream(newImage);
                    fo.write(baos.toByteArray());
                    fo.close();
                    Log.i(TAG, "Image saved!");
                    return true;
                }
            } else {
                Log.d(TAG, "Could not create directory.");
            }
        } else {
            Log.d(TAG, "External storage not available.");
        }
    } catch (Exception e) {

    }
    return false;
}

public static File getExternalStorageDir() {
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/"
            + Settings.EXT_STORAGE_DIRECTORY);
    return dir;
}

public static File getExternalStorageDir(String subdir) {
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/"
            + Settings.EXT_STORAGE_DIRECTORY + "/" + subdir);
    return dir;
}

保存它们后,我想为用户提供在默认图库应用中查看它们的可能性。在阅读了一些帖子后,我修改了以下代码:

MediaScannerConnectionClient mScanClient = new MediaScannerConnectionClient() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                try {
                    Log.d("onScanCompleted", uri + "success");
                    if (uri != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                } finally {
                    if (mScanCon != null)
                        mScanCon.disconnect();
                    mScanCon = null;
                }
            }

            @Override
            public void onMediaScannerConnected() {
                Log.i(TAG, "Media Scan Connected.");
                String[] files = Support.getExternalStorageDir("Photos")
                        .list();
                Log.i(TAG,
                        Support.getExternalStorageDir("Photos").list().length
                                + " elements in dir.");
                if (files.length > 0) {
                    for (String cur : files) {
                        if (cur.equals(".") || cur.equals(".."))
                            continue;
                        Log.i(TAG, "Using "
                                + cur
                                + " to scan stuff. "
                                + Support.getExternalStorageDir("Photos")
                                        .getAbsolutePath() + "/" + cur);
                        Log.i(TAG, "Not using "
                                + cur
                                + " to scan stuff. "
                                + Support.getExternalStorageDir("Photos")
                                        .toString() + "/" + cur);
                        mScanCon.scanFile(
                                Support.getExternalStorageDir("Photos")
                                        .getAbsolutePath() + "/" + cur,
                                "image/*");
                        break;
                    }
                } else {
                    Toast.makeText(getApplicationContext(),
                            "No images available.", Toast.LENGTH_LONG)
                            .show();
                }

            }
        };
        if (mScanCon != null)
            mScanCon.disconnect();
        mScanCon = new MediaScannerConnection(getApplicationContext(),
                mScanClient);
        mScanCon.connect();

奇怪的事情:似乎 onMediaScannerConnected 从未被解雇 - 有人知道吗?最近一个小时我一直在搜索网络和stackoverflow ..

谢谢你。

4

1 回答 1

0

您真的不必连接到媒体扫描仪来开始扫描,您可以改用这种静态方法

MediaScannerConnection.scanFile(context, new String[] {dir.getAbsolutePath()}, null, null);

编辑:

Uri uri = Uri.parse(filePath);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri, "image/*");
startActivity(i);
于 2013-03-04T13:53:27.380 回答