目前正在制作一个应用程序,其中有3个用于布局的按钮:“拍照”,“保存”和“签名”。 “拍摄照片”按钮用于从手机启动相机拍摄照片,一旦我拍摄了该照片,无论它是什么,都会显示在imageView中。保存按钮应该将该图像保存到图库中。不幸的是,当我运行我的应用程序时,我可以知道该图像正在保存,但是该图像未显示在布局或手机的图库中。我尝试了以下方法:
uses-permission android:name="android.permission.CAMERA"
(离开
这在我的清单中导致我的应用崩溃了如何解决此问题,使其在imageView上显示图像,但实际图像在画廊中却应有的样子?代码在下面发布。另外,我的最低SDK版本为15,而最高SDK版本为28。
Android:Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BusinessOwnerActivity"></activity>
<activity android:name=".DriverActivity"></activity>
<activity android:name=".CreateAccountActivity"></activity>
</application>
package com.example.businessdeliveryappv4;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
//launch this layout
public class DriverActivity extends AppCompatActivity
{
public static final int CAMERA_REQUEST = 10;
Button signatureButton, goBackButton, takePhotoButton, saveButton;
ImageView imageView;
Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.driver_activity);
goBackButton = (Button) findViewById(R.id.goBackButton);
signatureButton = (Button) findViewById(R.id.signatureButton);
takePhotoButton = (Button) findViewById(R.id.takePhotoButton); // take picture
saveButton = (Button) findViewById(R.id.saveButton); // save the picture to gallery
imageView = (ImageView) findViewById(R.id.imageView);// get access to the image view.
goBackButton.setVisibility(View.INVISIBLE);
String editTextUserName =
getIntent().getStringExtra("editTextUserName");
((TextView) findViewById(R.id.driverActivityTextView)).setText
("Driver: " + editTextUserName + ", " + "\n Customer: (Customer Name)" + "\n 6851 Almont cv \n Stone Mountain, GA 30087");
StrictMode.VmPolicy.Builder newbuilder = new
StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(newbuilder.build());
}
/**
* This method will be called when the Take Photo button is clicked.
*/
public void takePhotoButton(View v)
{
Toast.makeText(this, "Camera in Use", Toast.LENGTH_SHORT).show();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Get the file (just using our method)
File file = getOutputFile();
// Get the file's Uri
fileUri = Uri.fromFile(file);
// Ask the camera to save the output there
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// and then start the activity just as you did before
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
//this method will be called when the Save button is clicked. the purpose of this method is
//to save the photo in the gallery.
public void SavePhotoButton(View v)
{
// Get the file, same method as before
File file = getOutputFile();
try
{
// Try to open an output stream
FileOutputStream fos = new FileOutputStream(file);
// Get the image
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bm = drawable.getBitmap();
// save the image to the output stream
bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// Flush so nothing gets stuck in the cache
fos.flush();
// Close the output stream
fos.close();
// We have to let everybody know the gallery changed
addPhotoToGallery(Uri.fromFile(file));
}
catch (IOException ioe)
{
// This is not supposed to happen, so I'm printing the stack trace just
// in case we need to debug
ioe.printStackTrace();
}
Toast.makeText(this, "Photo saved in gallery", Toast.LENGTH_SHORT).show();
}
private void addPhotoToGallery(Uri imageUri)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(imageUri);
this.sendBroadcast(mediaScanIntent);
}
public File getOutputFile()
{
File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getPackageName());
// If the directory doesn't exist
if (!directory.exists())
{
// try and create it
if (!directory.mkdirs())
{
Log.e("ERROR", "Couldn't create the directory");
}
}
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
return new File(directory.getPath() + File.separator + "IMG" + timestamp + ".jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == CAMERA_REQUEST)
{
Uri photoUri = null;
if (data == null)
{
// This means it was able to save it where we asked
Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
photoUri = fileUri;
}
else
{
// This means it wasn't able, so I'm gonna print the path for you to see
Toast.makeText(this, "Image saved to: " + data.getData(), Toast.LENGTH_SHORT).show();
photoUri = data.getData();
}
try
{
// We have to use the one here, anyway
Bitmap cameraImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
// At this point, we have the image from the camera.
imageView.setImageBitmap(cameraImage);
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}