使用HashMap将图像传递给另一个活动

时间:2016-03-07 15:01:56

标签: android android-intent hashmap

我将相对布局转换为图像。我正在使用HashMap为该图像分配String,并将该字符串从第一个活动传递到第二个活动,我将有一个简单的共享按钮。

我在LogCat中看到了以下内容。

    03-07 10:18:04.685: E/WindowState(712): getStack: Window{2eba0e01 u0 Starting com.jobaka.www.jobaka} couldn't find taskId=205 Callers=com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedInner:9603 com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedLoop:8662 com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLocked:8604 com.android.server.wm.WindowManagerService.executeAppTransition:4107 
03-07 10:18:04.686: E/WindowState(712): getStack: Window{2eba0e01 u0 Starting com.jobaka.www.jobaka} couldn't find taskId=205 Callers=com.android.server.wm.WindowState.getDisplayContent:762 com.android.server.wm.WindowState.getDisplayId:767 com.android.server.wm.InputMonitor.updateInputWindowsLw:288 com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedInner:9936 
03-07 10:18:04.686: E/WindowState(712): getStack: Window{2eba0e01 u0 Starting com.jobaka.www.jobaka} couldn't find taskId=205 Callers=com.android.server.wm.DisplayContent.setTouchExcludeRegion:237 com.android.server.wm.WindowManagerService.setFocusedStackFrame:3957 com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedInner:10008 com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedLoop:8662 
03-07 10:18:04.686: E/WindowState(712): getStack: Window{2eba0e01 u0 Starting com.jobaka.www.jobaka} couldn't find taskId=205 Callers=com.android.server.wm.WindowState.getDisplayContent:762 com.android.server.wm.WindowState.getWindowList:1325 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2739 com.android.server.wm.WindowManagerService.removeWindowLocked:2640 
03-07 10:18:04.686: E/WindowState(712): getStack: Window{2eba0e01 u0 Starting com.jobaka.www.jobaka} couldn't find taskId=205 Callers=com.android.server.wm.WindowState.getDisplayContent:762 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2744 com.android.server.wm.WindowManagerService.removeWindowLocked:2640 com.android.server.wm.WindowManagerService.removeWindow:2562 
03-07 10:18:04.769: W/EGL_emulation(1030): eglSurfaceAttrib not implemented
03-07 10:18:04.769: W/OpenGLRenderer(1030): Failed to set EGL_SWAP_BEHAVIOR on surface 0xe294cc60, error=EGL_SUCCESS
03-07 10:18:05.153: W/InputMethodManagerService(712): Got RemoteException sending setActive(false) notification to pid 22613 uid 10064
03-07 10:18:05.216: D/AndroidRuntime(22716): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-07 10:18:05.217: D/AndroidRuntime(22716): CheckJNI is OFF
03-07 10:18:05.387: E/memtrack(22716): Couldn't load memtrack module (No such file or directory)
03-07 10:18:05.387: E/android.os.Debug(22716): failed to load memtrack module: -2
03-07 10:18:05.405: D/AndroidRuntime(22716): Calling main entry com.android.commands.am.Am
03-07 10:18:05.483: I/ActivityManager(712): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.jobaka.www.jobaka/.Main} from uid 0 on display 0
03-07 10:18:05.484: D/AndroidRuntime(22716): Shutting down VM
03-07 10:18:05.492: I/art(22716): Debugger is no longer active
03-07 10:18:05.842: W/OpenGLRenderer(1030): Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
03-07 10:18:06.883: D/OpenGLRenderer(22696): Render dirty regions requested: true
03-07 10:18:06.888: D/(22696): HostConnection::get() New Host Connection established 0xeb9888f0, tid 22696
03-07 10:18:06.902: D/Atlas(22696): Validating map...
03-07 10:18:06.927: D/libEGL(22696): loaded /system/lib/egl/libEGL_emulation.so
03-07 10:18:06.927: D/libEGL(22696): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-07 10:18:06.944: D/libEGL(22696): loaded /system/lib/egl/libGLESv2_emulation.so
03-07 10:18:06.952: D/(22696): HostConnection::get() New Host Connection established 0xe1116120, tid 22729
03-07 10:18:06.982: I/OpenGLRenderer(22696): Initialized EGL, version 1.4
03-07 10:18:07.017: D/OpenGLRenderer(22696): Enabling debug mode 0
03-07 10:18:07.079: W/EGL_emulation(22696): eglSurfaceAttrib not implemented
03-07 10:18:07.079: W/OpenGLRenderer(22696): Failed to set EGL_SWAP_BEHAVIOR on surface 0xe1119220, error=EGL_SUCCESS
03-07 10:18:08.552: D/TaskPersister(712): removeObsoleteFile: deleting file=205_task.xml
03-07 10:18:08.876: I/ActivityManager(712): Displayed com.jobaka.www.jobaka/.Main: +3s383ms (total +4s329ms)
03-07 10:18:08.902: I/Choreographer(22696): Skipped 102 frames!  The application may be doing too much work on its main thread.
03-07 10:18:09.645: I/Choreographer(22696): Skipped 44 frames!  The application may be doing too much work on its main thread.
03-07 10:18:32.638: W/AudioTrack(712): AUDIO_OUTPUT_FLAG_FAST denied by client
03-07 10:18:32.639: I/ActivityManager(712): START u0 {cmp=com.jobaka.www.jobaka/.DetailsActivity (has extras)} from uid 10064 on display 0
03-07 10:18:33.120: W/EGL_emulation(22696): eglSurfaceAttrib not implemented
03-07 10:18:33.121: W/OpenGLRenderer(22696): Failed to set EGL_SWAP_BEHAVIOR on surface 0xe05979c0, error=EGL_SUCCESS
03-07 10:18:33.326: I/ActivityManager(712): Displayed com.jobaka.www.jobaka/.DetailsActivity: +609ms
03-07 10:18:33.735: I/LatinIME(908): Starting input. Cursor position = 0,0
03-07 10:18:33.756: I/Choreographer(22696): Skipped 35 frames!  The application may be doing too much work on its main thread.
03-07 10:18:33.925: W/EGL_emulation(908): eglSurfaceAttrib not implemented
03-07 10:18:33.925: W/OpenGLRenderer(908): Failed to set EGL_SWAP_BEHAVIOR on surface 0xef907320, error=EGL_SUCCESS
03-07 10:18:36.018: W/AudioTrack(712): AUDIO_OUTPUT_FLAG_FAST denied by client
03-07 10:18:37.462: W/AudioTrack(712): AUDIO_OUTPUT_FLAG_FAST denied by client
03-07 10:18:37.465: I/ActivityManager(712): START u0 {cmp=com.jobaka.www.jobaka/.FullImage (has extras)} from uid 10064 on display 0
03-07 10:18:37.501: E/BitmapFactory(22696): Unable to decode stream: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
03-07 10:18:37.561: W/EGL_emulation(22696): eglSurfaceAttrib not implemented
03-07 10:18:37.561: W/OpenGLRenderer(22696): Failed to set EGL_SWAP_BEHAVIOR on surface 0xe051de60, error=EGL_SUCCESS
03-07 10:18:37.767: I/ActivityManager(712): Displayed com.jobaka.www.jobaka/.FullImage: +297ms

DetailsActivity.java(First Activity)

package com.jobaka.www.jobaka;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;

/**
 * Created by Psp on 2016-03-03.
 */
public class DetailsActivity extends AppCompatActivity {

    EditText etTop, etBottom;
    TextView tvTop, tvBottom;
    Button bCreate;
    String imgstring;

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

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        if(id != -1){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageBitmap(bitmap);
        }else{

        }

        //imageUri = Uri.parse(extras.getString("imageUri"));

        etTop = (EditText) findViewById(R.id.etTop);
        tvTop = (TextView) findViewById(R.id.tvTop);
        etTop.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                tvTop.setText(s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,int count, int after) {}

            public void onTextChanged(CharSequence s, int start,int before, int count) {}
        });

        etBottom = (EditText) findViewById(R.id.etBottom);
        tvBottom = (TextView) findViewById(R.id.tvBottom);
        etBottom.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable sb) {
                tvBottom.setText(sb.toString());
            }

            public void beforeTextChanged(CharSequence sb, int start,int count, int after) {}

            public void onTextChanged(CharSequence sb, int start,int before, int count) {}
        });

        bCreate = (Button) findViewById(R.id.bCreate);
        bCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RelativeLayout v = (RelativeLayout)findViewById(R.id.iFrame);
                Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                        Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                view.draw(canvas);

                HashMap<String, Bitmap> mbit = new HashMap<String, Bitmap>();
                mbit.put(imgstring, bitmap);

                Intent sendImage = new Intent(DetailsActivity.this, FullImage.class);
                sendImage.putExtra("key", imgstring);
                startActivity(sendImage);
            }
        });




    }


}

FullImage.java(第二个活动)

package com.jobaka.www.jobaka;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

public class FullImage extends AppCompatActivity {

    ImageView finalimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        String imgstring = intent.getExtras().getString("key");


        //String imgstring = startingIntent.getStringExtra("pic");
        Bitmap bitmap = BitmapFactory.decodeFile("imgstring");
        finalimage = (ImageView) findViewById(R.id.ivFull);
        finalimage.setImageBitmap(bitmap);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

你有这个

Bitmap bitmap = BitmapFactory.decodeFile("key");

key不是文件路径

如果值imgstring是您应该具有的文件路径