gpt4 book ai didi

android - 从 Camera Intent 中检索内容 Uri

转载 作者:行者123 更新时间:2023-11-29 00:39:53 26 4
gpt4 key购买 nike

我正在触发相机拍照的 Intent,如下所示:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri myPhoto = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"the_photo.jpg"));

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myPhoto);
startActivityForResult(cameraIntent, INTENT_GET_PICTURE_FROM_CAMERA);

现在我想要显示这张照片的缩略图。如果我存储“myPhoto”Uri,我可以准确地看到文件系统上的文件。但是,要使用 Media Store API,我需要 Content Uri 表单上的 Uri。无论如何可以从相机返回的 Intent 中检索它,如果没有,我如何将我的文件系统 Uri 转换为 android 内容 Uri。

请注意,我知道我可以只获取位图并对其进行解码,同时进行下采样。这不是我想要的。我可以查询所有的缩略图,我看到它存在,所以我想从系统中获取它。

谢谢

最佳答案

我通过对项目执行媒体扫描解决了这个问题。从那里我可以获得内容 Uri(下面代码中处理程序的额外复杂性是因为我需要在操作 View 时在 UI 线程上处理我的结果):

private Uri mCameraFilePath;
private Context mContext;
...

private void handlePictureFromCamera(Intent data) {

MyMediaScannerClient client = new MyMediaScannerClient(
mCameraFilePath.getPath(), new Handler(), new OnMediaScanned() {

@Override
public void mediaScanned(Uri contentUri) {
//Here I have the content uri as its now in the media store
}
});
MediaScannerConnection connection = new MediaScannerConnection(mContext, client);
client.mConnection = connection;
connection.connect();
}

private interface OnMediaScanned {
public void mediaScanned(Uri contentUri);
}

/**
* This is used to scan a given file (image in this case) to ensure it is added
* to the media store thus making its content uri available. It returns the
* content form Uri by means of a callback on the supplied handler's thread.
*/
private class MyMediaScannerClient implements MediaScannerConnectionClient {

private final String mPath;
private MediaScannerConnection mConnection;
private final OnMediaScanned mFileScannedCallback;
private final Handler mHandler;

public MyMediaScannerClient(String path, Handler handler, OnMediaScanned fileScannedCallback) {
mPath = path;
mFileScannedCallback = fileScannedCallback;
mHandler = handler;
}

@Override
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, null);
}

@Override
public void onScanCompleted(String path, final Uri uri) {

mConnection.disconnect();
mHandler.post(new Runnable() {

@Override
public void run() {
mFileScannedCallback.mediaScanned(uri);
}
});
}
}

关于android - 从 Camera Intent 中检索内容 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10090944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com