简体   繁体   中英

App showing a blank screen but Preview looks fine

So I've tried to write an application that saves relatively high-quality images but when I start the app in an emulator or my phone, it only shows a blank screen. In the Preview, it looks like it should look.

I have little experience in Java. Not much tho. I've tried a few things like changing the layout or what is displayed but nothing really worked or made any difference.

This is my 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>

This is the 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>

This is activity_main source code:

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;
    }
}

This is the 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>

And last but not least, it's source code:

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);
    }
}

There are no error messages. I want those buttons to show up. The rest should work just fine.

It seems like that the multiple MAIN entry points are confusing your opening of the app.

Make sure you are opening the app from the correct launcher icon.

You should see two icons from the same app in your launcher. There might be a blank layout for the DisplayImage Activity which you are opening. Thus the blank screen.

I am referring to these in the manifest file

<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>

I am also not that experienced with Android but have you tried removing the intent filter for DisplayImage in your manifest? You have two activities with this defined :

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

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

That tells Android that it can launch the application from either of them and since DisplayImage is declared first in the manifest, what you are seeing is an empty blank screen as no image has yet been loaded from MainActivity.

Check this out so that you have a better understanding of intents and intent filter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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