【问题标题】:Opening a screenshot image in another activity在另一个活动中打开屏幕截图
【发布时间】: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


【解决方案1】:

看来,这段代码崩溃了

  ImageView view= (ImageView) findViewById(R.id.newView);
    view.setImageBitmap(bm);

原因: viewnull

评论

之前我说过应用程序可能会因为bmnull 而崩溃。但是 cmets 中的@greenapps 说错了。这就是为什么我更改了我的帖子并删除了这个“可能”的选择。

【讨论】:

  • bm is null.。不,没问题。您可以使用 null 来“清除”一个 ImageView。
  • 您在编辑帖子时出错了。现在我的评论是无稽之谈。没有任何理由编辑您的帖子。
  • 如果我在主要活动的 ImageView 中打开图像,代码可以正常工作。只是不知道为什么它没有在另一个活动中打开
  • @greenapps ,专门为你服务的:)
  • 我的上帝……你不必忏悔……;-)。
【解决方案2】:

所以我通过 Intent 传递位图来解决这个问题。不过必须将位图压缩为字节。

public  void onSave(View view){
    Bitmap bm = getScreenShot(view);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    Intent previewIntent = new Intent(this, SavePicture.class);
    previewIntent.putExtra("Bitmap", byteArray);

    startActivity(previewIntent);
}

那么这就是接收活动

public class SavePicture extends AppCompatActivity{

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

    byte[] byteArray = getIntent().getByteArrayExtra("Bitmap");
    Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView saveView = (ImageView) findViewById(R.id.saveView);
    saveView.setImageBitmap(bm);
}

}

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多