本文介绍了使用Android的媒体扫描仪的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜所有
我尝试将图像保存到SD卡的使用和媒体扫描仪扫描新的保存文件,以便它可以立即有一个问题IM

的误差在下面的行

 新的String [] {file.toString()},NULL,//错误:文件不能被解析新MediaScannerConnection.OnScanCompletedListener(){
//错误:MediaScannerConnection.OnScanCompletedListener不能被解析为一个类型

这是我的code:

 公共无效saveToSDCard(位图位图,字符串名称){
        布尔mExternalStorageAvailable = FALSE;
        布尔mExternalStorageWriteable = FALSE;
        字符串状态= Environment.getExternalStorageState();
        如果(Environment.MEDIA_MOUNTED.equals(州)){
            mExternalStorageAvailable = mExternalStorageWriteable = TRUE;
            Log.v(TAG,SD卡仅可用于读取和写入
                    + mExternalStorageAvailable + mExternalStorageWriteable);
            saveFile的(位图名);
        }否则如果(Environment.MEDIA_MOUNTED_READ_ONLY.equals(州)){
            mExternalStorageAvailable = TRUE;
            mExternalStorageWriteable = FALSE;
            Log.v(TAG,SD卡仅可用于读取
                    + mExternalStorageAvailable);
        }其他{
            mExternalStorageAvailable = mExternalStorageWriteable = FALSE;
            Log.v(TAG,请插入SD卡保存广告
                    + mExternalStorageAvailable + mExternalStorageWriteable);
        }
    }    私人无效了saveFile(位图位图,字符串名称){        字符串文件名=名称;
        ContentValues​​值=新ContentValues​​();
        文件sdImageMainDirectory =新的文件(环境
                .getExternalStorageDirectory(),getResources()。的getString(
                R.string.directory));
        sdImageMainDirectory.mkdirs();
        文件OUTPUTFILE =新的文件(sdImageMainDirectory,文件名);
        values​​.put(MediaStore.MediaColumns.DATA,outputFile.toString());
        values​​.put(MediaStore.MediaColumns.TITLE,文件名);
        values​​.put(MediaStore.MediaColumns.DATE_ADDED,系统
                .currentTimeMillis());
        values​​.put(MediaStore.MediaColumns.MIME_TYPE,图像/ PNG);
        URI URI = this.getContentResolver()。插入(
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,                值);
        尝试{
            的OutputStream outStream = this.getContentResolver()
                    .openOutputStream(URI);
            bitmap.com preSS(Bitmap.Com pressFormat.PNG,95,outStream);            outStream.flush();
            outStream.close();
        //这是哪里有问题IM
        //告诉有关新文件的媒体扫描器,以便它
            //立即提供给用户。
            MediaScannerConnection.scanFile(这一点,
                    新的String [] {file.toString()},空,
                    新MediaScannerConnection.OnScanCompletedListener(){
                公共无效onScanCompleted(字符串路径,开放的URI){
                    Log.v(ExternalStorage,扫描+路径+:);
                    Log.v(ExternalStorage, - > URI =+ URI);
                }
            });
        }赶上(IOException异常五){
            //无法创建文件,很可能是因为外部存储
            //目前没有安装。
            Log.v(ExternalStorage,写入错误+文件,E);
        }
    }


解决方案

我得到这个与静态方法很好地工作 SCANFILE

这是我的code:

 字符串文件路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+/+文件名;
MediaScannerConnection.scanFile(这一点,新的String [] {}文件路径,NULL,NULL);

希望帮助...

hi to allim trying to save an image to an SDcard and im having a problem using the media scanner to scan the new saved file so that it is immediately available

the errors are in the following lines

new String[] { file.toString() }, null, // error: file cannot be resolved

new MediaScannerConnection.OnScanCompletedListener() {
// error: MediaScannerConnection.OnScanCompletedListener cannot be resolved to a type

this is my code:

public void saveToSDCard(Bitmap bitmap, String name) {
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
            Log.v(TAG, "SD Card is available for read and write "
                    + mExternalStorageAvailable + mExternalStorageWriteable);
            saveFile(bitmap, name);
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
            Log.v(TAG, "SD Card is available for read "
                    + mExternalStorageAvailable);
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
            Log.v(TAG, "Please insert a SD Card to save your Ad "
                    + mExternalStorageAvailable + mExternalStorageWriteable);
        }
    }

    private void saveFile(Bitmap bitmap, String name) {

        String filename = name;
        ContentValues values = new ContentValues();
        File sdImageMainDirectory = new File(Environment
                .getExternalStorageDirectory(), getResources().getString(
                R.string.directory));
        sdImageMainDirectory.mkdirs();
        File outputFile = new File(sdImageMainDirectory, filename);
        values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
        values.put(MediaStore.MediaColumns.TITLE, filename);
        values.put(MediaStore.MediaColumns.DATE_ADDED, System
                .currentTimeMillis());
        values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
        Uri uri = this.getContentResolver().insert(
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                values);
        try {
            OutputStream outStream = this.getContentResolver()
                    .openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);

            outStream.flush();
            outStream.close();
        // this is where im having the problem
        // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(this,
                    new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.v("ExternalStorage", "Scanned " + path + ":");
                    Log.v("ExternalStorage", "-> uri=" + uri);
                }
            });
        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.v("ExternalStorage", "Error writing " + file, e);
        }
    }
解决方案

I got this working nicely with the static method scanFile

And here is my code:

String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + fileName;
MediaScannerConnection.scanFile(this, new String[] { filePath }, null, null);

Hope that helps...

这篇关于使用Android的媒体扫描仪的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 09:45
查看更多