[英]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.