无法将图像保存在指定的文件位置

时间:2011-03-15 11:00:11

标签: android image file save

package com.ammu;

import java.io.File;
import java.lang.reflect.Field;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class camera extends Activity {
    /** Called when the activity is first created. */

    Button b1;
    TextView t1;
    ImageView im;
    protected String path;
    protected boolean taken;
    protected final static String photo_taken="taken";
            public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        im=(ImageView)findViewById(R.id.img);
        b1=(Button)findViewById(R.id.but);
        t1=(TextView)findViewById(R.id.txt);
        b1.setOnClickListener(new ButtonClickHandler());
        path=Environment.getExternalStorageDirectory()+"img/makemachineeg.jpeg";
            }
        public class ButtonClickHandler implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startCameraActivity();
            }

        }
        public void startCameraActivity() {
            // TODO Auto-generated method stub
            File file=new File(path);
            Uri output=Uri.fromFile(file);
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,output);
            startActivityForResult(intent,0);


        }

        public void onActivityResult(int requestcode,Intent data){
            //Log.i("MakeMachine","resultcode");
            Log.i("MakeMachine","resultcode:"+requestcode);
            switch(requestcode){
            case 0:
                    Log.i("makeMachine","Usercancelled");
                    break;
            case -1:
                    onPhotoTaken();
                    break;
            }
        }

        private void onPhotoTaken() {
            // TODO Auto-generated method stub
            taken=true;
            BitmapFactory.Options option=new BitmapFactory.Options();
            option.inSampleSize=4;
            Bitmap bitmap=BitmapFactory.decodeFile(path,option);
            im.setImageBitmap(bitmap);
            t1.setVisibility(View.GONE);

        }
        protected void onSaveStateInstance(Bundle outstate){
            outstate.putBoolean(camera.photo_taken,taken);
            }

        protected void onRestoreStateInstance(Bundle savedInstanceState){
            Log.i("MakeMachine","onRestoreInstanceState()");
            if(savedInstanceState.getBoolean(camera.photo_taken,taken)){
                onPhotoTaken();
            }
            }


}

1 个答案:

答案 0 :(得分:3)

您是否已获得android清单文件的许可?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />