【问题标题】:Show captured image in new activity在新活动中显示捕获的图像
【发布时间】:2016-02-08 18:32:53
【问题描述】:

我是 android 新手,正在构建一个小应用程序,可以从相机拍照并将其保存到图库。

这是捕获图像的功能。

private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

这是activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >

        <Button
            android:id="@+id/btnSelectPhoto"
            android:background="#149F82"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />
    </LinearLayout>



</LinearLayout>

我想在捕获图像时执行此操作,我想在另一个活动(页面)上显示图像,而不是在具有捕获图像按钮的同一活动上。如何做到这一点。

提前致谢

【问题讨论】:

  • 您需要将图像保存到设备而不是获取捕获图像的路径stackoverflow.com/questions/20327213/… 并将其传递给所需的活动
  • 您已经将图像保存在外部存储File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); 中,只需切换活动并在那里读取文件。
  • 从 onactivityResult 中的选取器意图中获取返回的数据 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("数据"); Uri tempUri = getImageUri(getApplicationContext(), photo);然后使用 bundle 将该数据传递给新的 Activity,然后显示该 bundle 中的图像。
  • @Murtaza Khursheed Hussain 如何切换活动,请您详细说明。
  • 检查我的答案并告诉我是否可行

标签: android


【解决方案1】:

您只需从您的方法中传递新活动的路径。

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("MyImagePath", destination.getAbsoluteFile());
startActivity(intent);

在新活动中

File imgFile = new  File(filePath);

if(imgFile.exists()){

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

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

【讨论】:

    【解决方案2】:

    很简单,拍摄照片后,您的照片将被保存。现在,在 Activity Result 方法上,您只需要使用意图即可转到您的第二个 Activity。

    在您的第二个活动中,只需获取已保存图像的路径并将其设置到您的 ImageView 中。

    【讨论】:

      【解决方案3】:

      MainActivity

       String filePath;
          @Override
              protected void onCreate(Bundle savedInstanceState) {   
              ...
              }
      
          private void onCaptureImageResult(Intent data) {
                  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                  thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
      
            String fileName = System.currentTimeMillis() + ".jpg"
      
            filePath =  Environment.getExternalStorageDirectory().getAbsolutePath() + fileName;
      
            File destination = new File(filePath);
      
            ...
            }
      
            yourButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                      Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
                      intent.putExtra("filePath", filePath)
                      startActivity(intent);
      
                      }
                  });
      

      另一个活动

      String filePath;
          @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
      
          Intent intent = this.getIntent();
      
          filePath = intent.getStringExtra("filePath");
      
          File imgFile = new  File(filePath);
      
          if(imgFile.exists()){
      
              Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
      
              ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
      
              myImage.setImageBitmap(myBitmap);
      
              }
           }
      

      【讨论】:

      • 当我写这行代码时,它给出了错误。 filePath = Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"; .(不兼容的类型)
      • 需要错误:java.lang.string 找到:java.io.File
      • 我删除了+".jpg" 但错误依旧存在
      • 好吧,我认为你的问题在这里 Environment.getExternalStorageDirectory() 但先试试这个:尝试将 filePath = Environment.getExternalStorageDirectory(), System.currentTimeMillis() 更改为:filePath = Environment.getExternalStorageDirectory() , "myNewImage.jpg"
      • 错误仍然存​​在。任何其他解决方案
      【解决方案4】:

      我假设您知道如何从意图中获取捕获的图像信息。

      如果你不知道。谷歌它,你会找到很多解决方案。

      check this one

      您可以采用两种方式在下一个活动中显示捕获的图像。

      1 => 启动要在其中显示图像的新活动。并在 onCreate 方法中打开相机,并在 onActivityResult 中从相机中获取结果并显示图像。

      2=> 打开相机并在 onActivityResult 中获取 Intent 数据并将此信息传递给您的下一个活动并在 onCreate 方法中显示图像。

      【讨论】:

        【解决方案5】:

        以下是一个解决方案。您可以将捕获的位图传递到一个意图中,然后开始您的新活动。比如下面这个。一旦从捕获中获取图像,您就可以在 onCaptureImageResult() 中实现此逻辑。

            Intent intent = new Intent(this, NewActivity.class);
            intent.putExtra("MyImage", bitmap);
            startActivity(intent);
        

        然后您可以按照以下步骤从意图中获取位图。并将图像设置为 ImageView 或用它做其他事情。

            ImageView imgView = (ImageView) img.findViewById(R.id.myIMageView); /* Called inside OnCreate() using the parent View */
            Intent intent = getIntent();
            Bitmap bitmap = (Bitmap) intent.getParcelableExtra("MyImage");
        

        希望对你有帮助。享受吧。

        【讨论】:

        • 图片太大会抛出异常
        猜你喜欢
        • 1970-01-01
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多