大家好!我正在使用相机将相片保存在手机库中。但我不想将照片保存在图库中。我想把它保存在其他地方。所以我需要的是不要自动将其保存在图库中。
这是我的代码:
public class MainActivity extends AppCompatActivity {
ImageView photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.btnPhoto);
photo = (ImageView) findViewById(R.id.imgPhoto);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
photo.setImageBitmap(bitmap);
}
}
如果我的问题不够好,请问我!请帮帮我。
答案 0 :(得分:0)
这种打开相机和路径的方法是File declare global。
private void openCamera() {
Logger.i("openCamera");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
File rootPath = new File(Environment.getExternalStorageDirectory(), "folderName");
if (!rootPath.exists())
rootPath.mkdirs();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
String snapshotImage = df.format(new Date()) + ".jpg";
rootPath = new File(rootPath + "/" + snapshotImage);
path = rootPath;
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(path));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
获得结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
FileOutputStream fos = null;
try {
// Float Latitude=0.0f, Longitude=0.0f;
imageDvr.setImageURI(Uri.parse(path.getAbsolutePath()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
}
}
并在清单文件中给出每个任务
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
您将在 onActivityResult()中获得图片的位图,然后您将按位图存储该图片。
在自制文件夹上使用以下功能存储图片。你需要制作那个文件夹并需要使用它的路径。
像这样。public void SaveImage(Bitmap showedImgae){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/YourSelfMadeFolder");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "FILENAME-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
Toast.makeText(activityname.this, "Image Saved", Toast.LENGTH_SHORT).show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
getApplicationContext().sendBroadcast(mediaScanIntent);
}
答案 2 :(得分:0)
您可以使用您的URI添加意图android.provider.MediaStore.EXTRA_OUTPUT:
private File getOutputMediaFile() {
File mediaStorageDir = null;
String state = Environment.getExternalStorageState();
if (state.contains(Environment.MEDIA_MOUNTED)) {
mediaStorageDir = new File(Environment
.getExternalStorageDirectory().toString() + "/yourFolderName");
} else {
mediaStorageDir = new File(Environment
.getExternalStorageDirectory().toString() + "/yourFolderName");
}
if (!mediaStorageDir.exists()) {
Logger.d("Desc", "File dir " + mediaStorageDir.mkdirs());
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"Image_" + timeStamp + ".jpg");
return mediaFile;
}
按下按钮:
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri mImageCaptureUri = null;
File mediaFile = getOutputMediaFile();
mImageCaptureUri = Uri.fromFile(mediaFile);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
this.startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
}}0;
关于活动结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FilePath","Path"+mediaFile.getAbsolutePath();)
}