从app / camera保存图像

时间:2015-07-06 11:28:22

标签: java android

我在MainActivity中有这个代码: onCreate():

#include <boost/atomic.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <vector>

namespace /*static*/ {
  boost::atomic<int> data;

  boost::barrier*           slave_thread_finished_barrier;
  boost::mutex              slave_thread_finished_mutex; 
  boost::condition_variable slave_thread_finished_cond;
  bool                      slave_thread_finished = false;

  struct Work {
    void signal_slave()
    {
      boost::lock_guard<boost::mutex> lock(data_ready_mutex);
      data_ready = true;
      cond.notify_all();
    }

    void slave_thread()
    {
      static boost::atomic_int _id_gen(0);
      id = _id_gen++;

      std::cout << "(" << id << ") slave_thread created\n";
      while (true) {

        boost::unique_lock<boost::mutex> lock(data_ready_mutex);
        cond.wait(lock, [&]{ return data_ready; });

        data_ready = false;

        data++;

        num_run++;

        slave_thread_finished_barrier->wait();

        // signaling the main thread that the slave threads are done.
        if (id == 0)
        {
          boost::lock_guard<boost::mutex> lock(slave_thread_finished_mutex);
          slave_thread_finished = true;
          slave_thread_finished_cond.notify_one();
        }

      }
    }

  private:
    int id = 0;
    bool data_ready = false;
    int  num_run = 0;

    boost::mutex data_ready_mutex;
    boost::condition_variable cond;

  };

}

#include <boost/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>

using hrc = boost::chrono::high_resolution_clock;

int main()
{
  boost::thread_group tg;

  size_t nThreads = 10;

  slave_thread_finished_barrier = new boost::barrier(nThreads);
  std::vector<Work> works(nThreads);

  for (size_t i = 0; i < nThreads; i++) {
    tg.create_thread(boost::bind(&Work::slave_thread, boost::ref(works[i])));
  }

  while (true) {
    auto start_time = hrc::now();

    for (auto& w : works)
      w.signal_slave();

    // Wait for slave threads to finish.
    boost::unique_lock<boost::mutex> lock(slave_thread_finished_mutex);
    slave_thread_finished_cond.wait(lock, [&]{ return slave_thread_finished; });
    slave_thread_finished = false;

    std::cout << "Elapsed Time = " << (hrc::now() - start_time) << std::endl;
  }

  tg.join_all();
}

public void onActivityResult(int reqCode,int resCode,Intent data):

mtestImage = (ImageView) findViewById(R.id.testImage);
mtestImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RESULT_LOAD_IMAGE);
            }
        });

它有效,ImageView向我显示我打开的图片,但是如果我重新启动Activity:

if (resCode == RESULT_OK) {
          if (reqCode == 1) {
             mtestImage.setImageURI(data.getData());
          }
      }

ImageView是恢复默认图片。我如何保存将来使用它的图像? (我正在尝试用新图片更改DrawerMenu的BG,用户可以选择)。 我在互联网上找到了一些代码,但我不确定如何将它与我的代码结合起来:

Intent intent = getIntent();
        finish();
        startActivity(intent);

谢谢!

1 个答案:

答案 0 :(得分:0)

Try this code.


private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), ".camerapics");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;

        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }


    mtestImage = (ImageView) findViewById(R.id.testImage);
mtestImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        fileUri = getOutputMediaFile();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                startActivityForResult(intent, RESULT_LOAD_IMAGE);
            }
        });


        public void onActivityResult(int reqCode, int resCode, Intent data)
{
if (resCode == RESULT_OK) {
          if (reqCode == RESULT_LOAD_IMAGE) {
            Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
            mtestImage.setImageBitmap(bmp);

setUsername(此,fileUri.getPath());

          }
      }
      }