1

デフォルトのカメラ アクティビティ (を使用) で高解像度の写真を撮りintent.put Extras、SD カードに保存しています。

コード:

public class CameraActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    Button takepicture ;
    ImageView iv ;
    TextView tv;
    Button show;

    String filepath;
    Intent i;
    Uri mUri;

    final static int cameraData = 0;

    File folder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        takepicture = (Button) findViewById(R.id.button1);
        iv = (ImageView) findViewById(R.id.imageView1);
        tv = (TextView) findViewById(R.id.textView1);
        show = (Button) findViewById(R.id.button2);
        takepicture.setOnClickListener(this);
        show.setOnClickListener(this);
    }

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

        switch(v.getId()){

        case R.id.button1:

            String sdcardstate = android.os.Environment.getExternalStorageState();

            if(sdcardstate.contentEquals(android.os.Environment.MEDIA_MOUNTED)){

                 filepath = Environment.getExternalStorageDirectory().getPath();

                 folder = new File(filepath,"wax");

                 if(!folder.exists()){
                     try {
                        folder.createNewFile();
                         Log.d("folder created", "ya");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                 }

                 mUri = Uri.fromFile(folder);
                 Log.d("bk", mUri.toString());

                 i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                 i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

                 Log.d("extra", "extra");
                 startActivityForResult(i,cameraData);
            }
            break;

        case R.id.button2:

            File f = new File(filepath,"bmp.png");

            Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());              

            iv.setImageBitmap(myBitmap);                
            break;
        }           
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK){

            tv.setText("Result ok");
            Log.d("ok", "ok");
            Bundle extras = data.getExtras();

            Bitmap bmp = (Bitmap) extras.get("data");
        }
    }
}

カメラのアクティビティが開始され、画像が撮影されますが、保存をクリックしても戻らず、強制終了します。

私はこれについてかなりの数のスレッドを読みましたが、カメラのアクティビティが開始される前にファイルを作成する必要があることを学びましたが、それでもそうではありません。

助けてください、私はこの問題で1週間ほど立ち往生しています。

Logcat エラー

06-15 16:05:50.205: W/dalvikvm(1780): threadid=10: thread exiting with uncaught exception (group=0x4001d800)
06-15 16:05:50.205: E/AndroidRuntime(1780): FATAL EXCEPTION: GLThread 12
06-15 16:05:50.205: E/AndroidRuntime(1780): java.lang.IllegalArgumentException: No configs match configSpec
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:760)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:916)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1246)
06-15 16:05:50.205: E/AndroidRuntime(1780):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
06-15 16:05:50.294: W/ActivityManager(59):   Force finishing activity com.android.camera/.Camera
06-15 16:05:50.444: V/camera(1780): stopPreview
4

4 に答える 4

2

これは outOfMemoryException の匂いがします。巨大な画像ファイルを直接取得する代わりに、メモリをすべて消費しないようにコード マジックを実行する必要があります。こちらのドキュメントをご覧ください: http://developer.android.com/training/displaying-bitmaps/index.html

そしていくつかのコード 4 u:

public Bitmap decodeFile(File f, int size){
    try {

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;

        while(true){
            if(width_tmp/2<size) // || height 
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}
于 2012-06-15T11:32:51.127 に答える