0
私は自分のアプリケーションでカメラを使って写真を撮ったときに自分のアプリケーション専用のアルバムを生成したいと思います。このアルバムは、アプリケーション内でカメラの意図からキャプチャしたすべての画像を保存するために使用されます。これはMessengerとWhatsappアプリのように機能します。誰でもこれを行う方法を知っていますか?MessengerとWhatsappアプリのようにカメラから画像を保存する自分のアプリアルバムを作成
public class ImageQueryActivity extends AppCompatActivity {
private LinearLayout mImageQueryLayout;
private ImageView mAddPhotoButton;
private String mCurrentPhotoPath;
final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_GALLERY = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_query);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mImageQueryLayout = (LinearLayout) findViewById(R.id.image_query_layout);
mAddPhotoButton = (ImageView) findViewById(R.id.add_photo_image_view);
mAddPhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
// dp to pixel converter
public static int convDpToPx(Context context, float dp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
AlertDialog.Builder builder = new AlertDialog.Builder(ImageQueryActivity.this);
builder.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(),
"Problem occurred while saving photo",
Toast.LENGTH_LONG).show();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
"com.kelvin.foodizzy.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
galleryAddPic(mCurrentPhotoPath);
}
}
}
});
builder.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setType("image/*").setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), REQUEST_GALLERY);
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
convDpToPx(getApplicationContext(), 180));
layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin,
layoutParams.rightMargin, convDpToPx(getApplicationContext(), 20));
imageView.setLayoutParams(layoutParams);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
galleryAddPic(mCurrentPhotoPath);
mImageQueryLayout.addView(imageView);
}
}
}
private void galleryAddPic(String file) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(file);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
}
あなたは基本的にフォルダの作成方法を尋ねています。 –
画像も保存したいです。現在のところ、Photosアプリで画像を取得できません。 以下は私のfile_paths.xmlです: –
<?xml version = "1.0" encoding = "utf-8"?> <外部パス 名=" my_images」 パス= "アンドロイド/データ/ com.kelvin.foodizzy /ファイル/写真" /> –