权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />`
我从相机点击后尝试保存照片,将图像文件保存到
文件夹(SD卡:工作正常,空间很大),并从该目录中检索,但保存的文件大小为零,因此无法查看照片。
我尝试了所有可能的方法,但它显示零字节。所有工作正常,只有图像大小的问题。
任何帮助都会有用。
public class CameraScan extends AppCompatActivity {
private ImageView imageHolder;
String mCurrentPhotoPath;
android.support.v7.app.ActionBar actionBar;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_scan);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
// TODO: Remove the redundant calls to getSupportActionBar()
// and use variable actionBar instead
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
imageHolder = (ImageView) findViewById(R.id.captured_photo);
Button capturedImageButton = (Button) findViewById(R.id.photo_button);
if(isSdPresent()==true)
{
capturedImageButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
else
{
Toast.makeText(this,"Insert SDCard", Toast.LENGTH_LONG).show();
}
}
//get the thumbnail
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageHolder.setImageBitmap(imageBitmap);
}
}
//save fullsize photo
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "KAWACH_" + 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();
setPic();
galleryAddPic();
return image;
}
//take pic wd camera app
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.darklord.kawach",
photoFile);
Bundle data = new Bundle();
data.putString(MediaStore.EXTRA_OUTPUT, photoURI.toString());
//takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
//adding to gallery
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
//seting image
private void setPic() {
// Get the dimensions of the View
int targetW = imageHolder.getWidth();
int targetH = imageHolder.getHeight();
// Get the dimensions of the bitmap
try {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW,photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
//imageHolder.setImageBitmap(bitmap);
if(photoW*photoH > targetW*targetH){
int imageSizef = (int)(photoW*photoH/targetW*targetH);
String imageSize = imageSizef+"";
bmOptions.inSampleSize = Integer.parseInt(imageSize);
bmOptions.inJustDecodeBounds = false;
imageHolder.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions));
}else{
imageHolder.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions ));
}
}catch (Exception e){
Log.d("Error",""+e);
}
}
//sd present or not
public static boolean isSdPresent()
{
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent myIntent = new Intent(getApplicationContext(), KawachHome.class);
startActivityForResult(myIntent, 0);
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_gallery:
// User chose the "Favorite" action, mark the current item
// as a favorite...
Intent i = new Intent(this,Kawach_gallery.class);
this.startActivity(i);
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}