繁体   English   中英

应用显示空白屏幕,但预览效果很好

[英]App showing a blank screen but Preview looks fine

因此,我试图编写一个保存相对高质量图像的应用程序,但是当我在模拟器或手机中启动该应用程序时,它仅显示空白屏幕。 在预览中,看起来应该看起来像。

我没有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-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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM