如何通过相机单击后将图片保存在图像视图中?

时间:2016-06-19 07:43:46

标签: android camera

我正在制作一个Android应用程序我需要在点击后保存图片,假设我点击图像,然后图像将保存并出现在图像视图之后,当我再次点击图片然后第一张图片将自动删除最新的将保存。 这是我运行相机的代码:

public class MainActivity extends AppCompatActivity{
    Button b1;
    ImageView iv;
    private static final int CAMERA_REQUEST = 1888;
SharedPreferences myPrefrence;
    String itemNAme;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button)findViewById(R.id.button);
        iv=(ImageView)findViewById(R.id.imageView);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            iv.setImageBitmap(bp);

            SharedPreferences.Editor editor = myPrefrence.edit();
            editor.putString("namePreferance", itemNAme);
            editor.putString("imagePreferance", encodeTobase64(bp));
            editor.commit();
        }


    }

    public String encodeTobase64(Bitmap bp ) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();

        String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        return imageEncoded;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:2)

我已更新您的代码,请参阅下文并尝试使用。它会将图像保存在ImageView

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Button button;

    Bitmap bitmap;

    private static final int PICK_IMAGE = 100;
    SharedPreferences myPrefrence;
    String itemNAme;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, PICK_IMAGE);
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
            Uri imageUri = data.getData();


            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

                // Here you can set to you direct to your imageView

                imageView.setImageBitmap(bitmap);

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


            final ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] byteArray = stream.toByteArray();

            String saveThis = Base64.encodeToString(byteArray, Base64.DEFAULT);


            SharedPreferences.Editor editor = myPrefrence.edit();
            editor.putString("namePreferance", itemNAme);
            editor.putString("imagePreferance", saveThis);
            editor.commit();


        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}