package com.example.cam_tab;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity
{
protected Button _button;
protected ImageView _image;
protected TextView _field;
protected String _extStorageDirectory;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_image = ( ImageView ) findViewById( R.id.imageView1 );
_field = ( TextView ) findViewById( R.id.textView1 );
_button = ( Button ) findViewById( R.id.button1 );
_button.setOnClickListener( new ButtonClickHandler() );
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/draw/Images");
folder.mkdirs();
_extStorageDirectory = folder.toString();
}
public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view )
{
Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
startCameraActivity();
}
}
protected void startCameraActivity()
{
Log.i("cam_tab", "startCameraActivity()" );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
File file = new File( _extStorageDirectory, System.currentTimeMillis()+"draw.jpg");
Uri outputFileUri = Uri.fromFile( file );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
FileOutputStream fos = null;
{
try {
fos = new FileOutputStream(file);
if (fos != null)
{
Bitmap mMergedLayersBitmap = null;
mMergedLayersBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
fos.flush();
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i( "cam_tab", "resultCode: " + resultCode );
switch( resultCode )
{
case 0:
Log.i( "cam_tab", "User cancelled" );
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
Log.i( "cam_tab", "onPhotoTaken" );
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
_field.setVisibility( View.GONE );
}
@Override
protected void onRestoreInstanceState( Bundle savedInstanceState){
Log.i( "cam_tab", "onRestoreInstanceState()");
if( savedInstanceState.getBoolean( MainActivity.PHOTO_TAKEN ) ) {
onPhotoTaken();
}
}
@Override
protected void onSaveInstanceState( Bundle outState ) {
outState.putBoolean( MainActivity.PHOTO_TAKEN, _taken );
}
}
我无法将图像保存在文件夹中。请支持我的解决方案。 提前致谢。我尝试了很多选项,但它总是显示我的错误。因为,我是第一次使用android,我无法纠正它。任何帮助都会很明显
答案 0 :(得分:0)
您需要检查路径是否可用。如果不是,则需要创建父目录
File file = new File(Environment.getExternalStorageDirectory() +File.separator + "/picture/" + ".png");
if(!file.exists()){
file.mkdirs();
}
只需更新此代码。