我正在開發一個應用程序,它會拍攝一張照片,然後將其返回並放入一個imageview中。 我能夠得到它的工作時,它會返回縮略圖大小的圖像。我遵循Android開發者頁面上的指示創建一個圖像文件,但由於某種原因,它不適合我。我有一個按鈕,在啓動intent之前啓動一個調用createImageFile()的方法。然而,這種方法失敗,相機甚至從未啓動。創建相機應用程序存儲照片的目錄時出錯
這裏是我的版本:
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").
format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Log.i("storage: ",storageDir.getAbsolutePath());
File image = File.createTempFile(imageFileName,
".jpg",storageDir);
Log.i("filename: ", "afterimage");
FILENAME = "file:" + image.getAbsolutePath();
Log.i("filename: ", FILENAME);
return image;
}
但是我的代碼永遠不會使得過去的文件圖像= File.createTempFile(映像文件名稱, 「.JPG」,storageDir) 具有因我從來沒有看到以下Log.i ();
我試圖用context.getCacheDir()創建storageDir,並且相機實際上啓動了,但在拍攝照片後,我無法點擊複選標記並返回主活動。
下面是活動的其餘部分如果需要的話:
public class Camera extends Activity
{
private final int PICTURE_ACTIVITY_CODE = 1;
String FILENAME;
File f;
Button b;
ImageView image;
@Override
/**
* onCreate method. Instantiates buttons and textfield
* and sets a listener for button that launches launchTakePhoto
* method.
*/
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
image = (ImageView) findViewById(R.id.img);
b = (Button) findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
launchTakePhoto();
}
});
}
/**
* method that finds storage location on devices. Then
* creates a file and launches an actvitiy for result
* to retrieve the data later and store it in that file,
* in that storage space.
*/
private void launchTakePhoto()
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
{
try
{
f = createImageFile();
}
catch(IOException e)
{
Log.i("Error","error");
}
}
else
{
f = new File(getCacheDir(), FILENAME);
}
if (f != null)
{
Uri outputFileUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, PICTURE_ACTIVITY_CODE);
}
}
protected void onActivityResult(int req, int res, Intent data)
{
if ((req == PICTURE_ACTIVITY_CODE)&&(res == RESULT_OK))
{
Log.i("res: ", "PASSED"); ImageView iv = (ImageView) findViewById(R.id.img);
Uri uriImage = Uri.fromFile(f);
iv.setImageURI(uriImage);
}
}
感謝您的幫助。我見過很多不同的可能答案,但目前爲止我沒有任何工作可以解決。我只是想能夠返回一個全尺寸的圖像,並將其放置在圖像保持器中。