【发布时间】:2017-03-28 10:02:17
【问题描述】:
我创建了一个应用程序来截取当前屏幕的屏幕截图,然后在新活动中打开图像。由于某种原因,当我想打开新活动来查看图片时,我的应用程序崩溃了。这是我的代码:
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
这是获取图像的 onSave 方法,用于在另一个活动中设置和显示图像:
public void onSave(View view){
Bitmap bm = getScreenShot(view);
ImageView view= (ImageView) findViewById(R.id.newView);
view.setImageBitmap(bm);
Intent saveIntent = new Intent(this, SavePicture.class);
startActivity(saveIntent);
}
这是 saveIntent 活动 XML 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/saveView"
android:contentDescription=""
tools:ignore="ContentDescription" />
</RelativeLayout>
还有清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.krypttech.gallery">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SavePicture">
</activity>
</application>
</manifest>
【问题讨论】:
-
你能显示崩溃堆栈跟踪吗?
-
查看link。在这里,您需要使用意图将截取的 uri 路径传递给下一个活动,并使用 getIntent() 读取新活动中的 uri。
-
引起:com.krypttech.gallery.MainActivity.onSave(MainActivity.java:207) 处的 java.lang.NullPointerException
-
@W.Bright 发布您的完整堆栈跟踪日志
-
ImageView view已经在View view的范围内定义(你的函数的参数onSave)。所以尝试重命名它。
标签: android image android-intent bitmap