Android仅隐藏图库中的图像

时间:2014-08-13 14:17:50

标签: android image-gallery

我创建了一个可以查看图像的应用程序。我面临的问题是当我使用" .nomedia"在文件夹中它隐藏了图库中的图像,图像也隐藏在我的应用程序中。我需要一个方法,帮助图像可以保持隐藏在库中,但在我的应用程序中显示。我正在使用文件夹路径访问图像。

代码:

public class SdActivity extends Activity implements
MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH;
private static final String FILE_TYPE = "*/*";

private MediaScannerConnection conn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File folder = new File("/sdcard/DCIM");
    allFiles = folder.list();
    // uriAllFiles= new Uri[allFiles.length];
    for (int i = 0; i < allFiles.length; i++) {
        Log.d("all file path" + i, allFiles[i] + allFiles.length);
    }
    // Uri uri= Uri.fromFile(new
    //     File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
    SCAN_PATH = Environment.getExternalStorageDirectory().toString()
            + "/yourfoldername/" + allFiles[0];
    Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
    Button scanBtn = (Button) findViewById(R.id.buttonLoadPicture);
    scanBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startScan();
        }
    });
}

private void startScan() {
    Log.d("Connected", "success" + conn);
    if (conn != null) {
        conn.disconnect();
    }
    conn = new MediaScannerConnection(this, this);
    conn.connect();
}

@Override
public void onMediaScannerConnected() {
    Log.d("onMediaScannerConnected", "success" + conn);
    conn.scanFile(SCAN_PATH, FILE_TYPE);
}

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

0 个答案:

没有答案