应用程序显示空白屏幕但预览看起来不错

App showing a blank screen but Preview looks fine

所以我尝试编写一个应用程序来保存相对高质量的图像,但是当我在模拟器或我的 phone 中启动该应用程序时,它只显示一个空白屏幕。在预览中,它看起来应该是这样。

我在Java方面经验不多。不多。我尝试了一些方法,例如更改布局或显示的内容,但没有任何效果或产生任何影响。

这是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cameraexample">

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".DisplayImage">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:authorities="com.example.android.fileprovider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
             <meta-data
                 android:name="android.support.FILE_PROVIDER_PATHS"
                 android:resource="@xml/file_path" />
        </provider>
    </application>
</manifest>

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/capture_image"
        android:onClick="captureImage"
        android:layout_gravity="center_horizontal"/>

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/display_image"
        android:onClick="displayImage"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

这是activity_main源代码:

public class MainActivity extends AppCompatActivity {

    String currentImagePath = null;
    private static final int IMAGE_REQUEST = 1;

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

    public void captureImage(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager()) != null){
            File imageFile = null;

            try {
                imageFile = getImageFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            if(imageFile != null) {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);

            }
        }
    }

    public void displayImage(View view) {
       Intent intent = new Intent(this, DisplayImage.class);
       intent.putExtra("image_path", currentImagePath);
       startActivity(intent);
    }

    private File getImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String imageName = "jps_" + timeStamp + "_";
        File storageDir  = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        return imageFile;
    }
}

这是activity_display_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayImage">

    <ImageView
        android:contentDescription="@string/bild_ansicht"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp"
        android:id="@+id/mimageView"/>
</RelativeLayout>

最后但同样重要的是,它是源代码:

public class DisplayImage extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_image);
        imageView = findViewById(R.id.mimageView);

        Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("image_path"));
        imageView.setImageBitmap(bitmap);
    }
}

没有错误消息。我希望这些按钮显示出来。其余的应该工作得很好。

多个 MAIN 入口点似乎让您无法打开应用程序。

确保您从正确的启动器图标打开应用程序。

您应该会在启动器中看到来自同一应用的两个图标。您打开的 DisplayImage Activity 可能有一个空白布局。因此黑屏。

我指的是清单文件中的这些

<activity android:name=".DisplayImage">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

我也没有使用 Android 的经验,但是您是否尝试过删除清单中 DisplayImage 的 Intent 过滤器? 您有两个活动定义为:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

这告诉 Android 它可以从它们中的任何一个启动应用程序,并且由于 DisplayImage 在清单中首先声明,您看到的是一个空的空白屏幕,因为尚未从 MainActivity 加载任何图像.

检查一下,以便您更好地了解意图和意图过滤器

https://developer.android.com/guide/components/intents-filters