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 /文件/圖片」/> –