画像をキャプチャしたいアプリケーションを作成し、その画像を電子メールで添付ファイルとして送信したいと考えています。
android.provider.MediaStore.ACTION_IMAGE_CAPTURE
インテント アクションを使用してカメラを開き、ファイルの Uri をパラメーターとして渡してEXTRA_OUTPUT
、画像をファイルに戻しています。external storage uri
これは完全に機能しており、 を使用するとキャプチャされた画像を取得できますEXTRA_OUTPUT
が、データフォルダー uri を使用すると機能せず、カメラが閉じず、すべてのボタンが機能しません。
外部ストレージディレクトリで結果を取得するための私のコードは次のとおりです
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
そして、このコードは、データフォルダー内の画像を取得するためのものです
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = getFilesDir();
out = new File(out, MyPharmacyOptions.PRESCRIPTION_IMAGE_NAME);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
3 番目のアプリケーションがデータ フォルダーにアクセスできないことはわかっていたので、これが問題を引き起こす可能性があるため、ファイルを共有するコンテンツ プロバイダーを 1 つ作成しました。
これが私のコンテンツ提供クラスです
public class MyContentProvider extends ContentProvider {
private static final String Tag = RingtonContentProvider.class.getName();
public static final Uri CONTENT_URI = Uri
.parse("content://x.y.z/");
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();
static {
MIME_TYPES.put(".mp3", "audio/mp3");
MIME_TYPES.put(".wav", "audio/mp3");
MIME_TYPES.put(".jpg", "image/jpeg");
}
@Override
public boolean onCreate() {
return true;
}
@Override
public String getType(Uri uri) {
String path = uri.toString();
for (String extension : MIME_TYPES.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES.get(extension));
}
}
return (null);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(getContext().getFilesDir(), uri.getPath());
if (f.exists()) {
return (ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY));
}
throw new FileNotFoundException(uri.getPath());
}
@Override
public Cursor query(Uri url, String[] projection, String selection,
String[] selectionArgs, String sort) {
throw new RuntimeException("Operation not supported");
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
File file = new File(getContext().getFilesDir(), uri.getPath());
if(file.exists()) file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Uri.fromFile(file);
}
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
File f = new File(getContext().getFilesDir(), "image1.jpg");
if(f.exists()) f.delete();
f = new File(getContext().getFilesDir(), "image2.jpg");
if(f.exists()) f.delete();
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
}
}
したがって、このコンテンツ プロバイダーを使用するには、次のコードを使用して uri をカメラ アクティビティに渡します。
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = MyContentProvider.CONTENT_URI;
uri = Uri.withAppendedPath(uri, imagename);
getContentResolver().insert(uri, null);
getContentResolver().notifyChange(RingtonContentProvider.CONTENT_URI, null);
Log.d(Tag, uri.toString());
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(i, CAMERA_RESULT);
ここで、外部ストレージ ディレクトリ以外の URL を渡すと、カメラは開いていますが、エミュレータでは閉じていませんが、デバイスではカメラが閉じようとしていますが、結果が得られません。
マニフェスト ファイルでこのコンテンツ プロバイダーを宣言しました
<provider
android:name=".contentproviders.MyContentProvider"
android:authorities="x.y.z" />
また、外部ストレージへの書き込みとカメラの使用も許可しています。
外部ストレージを使用して画像をキャプチャすることはできますが、外部ストレージが使用できない場合に画像をキャプチャしてメールを送信したいため、外部ストレージではなくデータディレクトリに画像を保存したいと考えています。
コンテンツ プロバイダーを作成すると、イメージを電子メール アプリケーションに共有することもできます。
カメラの意図でエクストラを提供しない場合、アクティビティ結果のバイト[]として画像をデータエクストラとして返しますが、これはサムネイルのためであるため、この方法を使用して高解像度の画像を取得することはできません.