0

该项目有两个活动,即“activity_view_message.xml”first activity和“activity_full_screen_message_image.xml”- second activity

我想要的是:

在第一个活动中,有一个 Android 'imageView' 来显示一个带有 'Picasso' 库的 'URI 图像'。imageview我需要在第二个活动的“Photoview”小部件中以全屏模式显示该“URI 图像”的“onClick”事件。

问题:

大多数图像都可以使用我的代码。但是肖像图像或高分辨率图像正在加载系统抛出此异常:

'Throwing OutOfMemoryError "Failed to allocate a 518400012 byte allocation with 16773984 free bytes and 207MB until OOM"' 

这是我的代码:

activity_view_message.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:id="@+id/activity_view_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="tekhinno.com.au.mygulukose.activities.ViewMessageActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/linear_border"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="12dp"
                android:orientation="horizontal">

                <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/view_message_channel_img"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/sm_human"
                    app:civ_border_color="#FF000000"
                    app:civ_border_width="0.5dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/view_message_channel_name_txt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="John Doe"
                            android:textColor="@android:color/black"
                            android:textSize="12sp" />

                        <TextView
                            android:id="@+id/view_message_duration_txt"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="2"
                            android:text="3h Ago"
                            android:textAlignment="textEnd"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <TextView
                        android:id="@+id/view_message_time_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:text="Jan 6 at 4.55 PM"
                        android:textSize="12sp" />

                </LinearLayout>

            </LinearLayout>

            <TextView
                android:id="@+id/view_message_message_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="12dp"
                android:text="Daily dose of proper pills"
                android:textColor="@android:color/black"
                android:textSize="12sp" />


            <ImageView
                android:id="@+id/view_message_image"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                android:layout_marginTop="12dp"
                android:background="@android:color/darker_gray" />

            <!--<VideoView-->
                <!--android:id="@+id/view_message_video_view"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="256dp"-->
                <!--android:layout_marginTop="12dp" />-->

            <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
                android:layout_width="match_parent"
                android:layout_height="250sp"
                android:id="@+id/video_player"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="48dp" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

activity_full_screen_message_image.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_view_full_screen_message_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tekhinno.com.au.mygulukose.ViewFullScreenMessageImage"
    android:orientation="vertical">

        <ProgressBar
            android:id="@+id/image_full_screen_progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
        </ProgressBar>

        <com.github.chrisbanes.photoview.PhotoView
            android:id="@+id/photo_view"
            android:layout_width="match_parent"
            android:layout_centerInParent="true"
            android:layout_height="match_parent"
            android:scaleType="center"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_alignParentBottom="true"
            android:textColor="@android:color/white"
            android:layout_gravity="bottom|center_horizontal"
            android:id="@+id/tv1"
            android:background="@android:color/black"
            android:alpha="0.5"
            android:paddingLeft="7dp"
            android:paddingRight="7dp"/>



</FrameLayout>

ViewMessageActivity.java

package tekhinno.com.au.mygulukose.activities;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.squareup.picasso.Picasso;

import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import tekhinno.com.au.mygulukose.R;
import tekhinno.com.au.mygulukose.ViewFullScreenMessageImage;
import tekhinno.com.au.mygulukose.alerts.EveryFifteenMinuteAlert;
import tekhinno.com.au.mygulukose.beans.msg_beans.Message;
import tekhinno.com.au.mygulukose.common.Session;
import tekhinno.com.au.mygulukose.rest.RestCommon;
import tekhinno.com.au.mygulukose.rest.msg_end_point.MessageEndPoint;

import static fm.jiecao.jcvideoplayer_lib.JCVideoPlayer.SCREEN_WINDOW_FULLSCREEN;

public class ViewMessageActivity extends BaseActivity {

    Integer idPatient;
    Context context;
    Integer idMessage;
    //private JCVideoPlayerStandard jcVideoPlayerStandard;

    @InjectView(R.id.view_message_channel_name_txt)
    TextView channelNameTextView;

    @InjectView(R.id.view_message_duration_txt)
    TextView durationTextView;

    @InjectView(R.id.view_message_time_txt)
    TextView timeTextView;

    @InjectView(R.id.view_message_message_txt)
    TextView messageTextView;

    @InjectView(R.id.view_message_channel_img)
    ImageView channelLogoImageView;

    @InjectView(R.id.view_message_image)
    ImageView messageImageView;

//    @InjectView(R.id.view_message_video_view)
//    VideoView messageVideoView;

    @InjectView(R.id.video_player)
    JCVideoPlayerStandard jcVideoView;

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

        ButterKnife.inject(this);
        init();

        context = this;

        final Intent intent = getIntent();

        DateFormat dateFormat = new SimpleDateFormat("MMM dd");
        DateFormat sdf = new SimpleDateFormat("HH:mm aaa");
        DateFormat realFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

        if (intent.getIntExtra("idPatient", 0) != 0) {
            idPatient = intent.getIntExtra("idPatient", 0);
        }

        if (intent.getStringExtra("channelName") != null || !intent.getStringExtra("channelName").equals("")) {

            channelNameTextView.setText(intent.getStringExtra("channelName"));
        } else {
            channelNameTextView.setText("");
        }

        if (intent.getStringExtra("channelLogo") != null || !intent.getStringExtra("channelLogo").equals("")) {
            Picasso.with(context).load(intent.getStringExtra("channelLogo")).into(channelLogoImageView);
        } else {
            channelLogoImageView.setVisibility(View.GONE);
        }

        Log.d("VIEW_MESSAGE"," LAST_UPDATED TRUE");
        //intent.getStringExtra("lastUpdated")

        Log.d("VIEW_MESSAGE"," LAST_UPDATED TRUE "+intent.getStringExtra("lastUpdated"));

        if (intent.getStringExtra("lastUpdated") != null || !intent.getStringExtra("lastUpdated").equals("")) {

            Log.d("VIEW_MESSAGE"," LAST_UPDATED TRUE SECOND");

            try {

                Date lastUpdated = realFormat.parse(intent.getStringExtra("lastUpdated"));

                String dateYear = dateFormat.format(lastUpdated);
                String hoursMinutes = sdf.format(lastUpdated);

                Log.d("VIEW_MESSAGE"," LAST_UPDATED TRUE DATE "+dateYear);

                String realTime = dateYear + " at " + hoursMinutes;
                timeTextView.setText(realTime);

                Date nowDate = new Date();

                DateTime lastTime = new DateTime(lastUpdated);
                DateTime nowDateTime = new DateTime(nowDate);

                Duration duration = new Duration(lastTime, nowDateTime);
                StringBuilder stringBuilder = new StringBuilder();

                if (duration.toStandardDays().getDays() > 0) {
                    stringBuilder.append(duration.toStandardDays().getDays() + " Days Ago");
                } else {
                    if (duration.toStandardHours().getHours() > 0) {
                        stringBuilder.append(duration.toStandardHours().getHours() + "h Ago");
                    } else {
                        if (duration.toStandardMinutes().getMinutes() > 0) {
                            stringBuilder.append(duration.toStandardMinutes().getMinutes() + " Min Ago");
                        } else {
                            if (duration.toStandardSeconds().getSeconds() > 0) {
                                stringBuilder.append(duration.toStandardSeconds().getSeconds() + " Seconds Ago");
                            }
                        }
                    }
                }

                String durate = stringBuilder.toString();

                Log.d("VIEW_MESSAGE"," DURATION "+durate);

                if (durate != null) {
                    durationTextView.setText(durate);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }

        } else {

            Log.d("VIEW_MESSAGE"," LAST_UPDATED FALSE");

            durationTextView.setText("");
            timeTextView.setText("");

        }

        if (intent.getStringExtra("message") != null || !intent.getStringExtra("message").equals("")) {
            messageTextView.setText(intent.getStringExtra("message"));
        }

        if (intent.getStringExtra("imageURL") == null || intent.getStringExtra("imageURL").equals("")) {
            messageImageView.setVisibility(View.GONE);
        } else {
            messageImageView.setVisibility(View.VISIBLE);
            final String imageUrl = intent.getStringExtra("imageURL") ;
            Picasso.with(context).load(imageUrl).fit().into(messageImageView);

            messageImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(getApplicationContext(), "you click on the image", Toast.LENGTH_LONG).show();
                    Intent intent1 = new Intent(getApplicationContext(), ViewFullScreenMessageImage.class);
                    intent1.putExtra("imageUrl",intent.getStringExtra("imageURL"));
                    intent1.putExtra("messageContent", intent.getStringExtra("message"));
                    intent1.putExtra("imageTitle",intent.getStringExtra("channelName"));
                    //Toast.makeText(getApplicationContext(), "you click on the image" + intent1.getStringExtra("imageTitle"), Toast.LENGTH_LONG).show();
                    startActivity(intent1);

                }
            });
        }

//        if (intent.getStringExtra("videoURL") == null || intent.getStringExtra("videoURL").equals("")) {
//            messageVideoView.setVisibility(View.GONE);
//        } else {
//            messageVideoView.setVisibility(View.VISIBLE);
//
//            Uri uri = Uri.parse(intent.getStringExtra("videoURL"));
//            messageVideoView.setMediaController(new MediaController(context));
//            messageVideoView.setVideoURI(uri);
//            messageVideoView.requestFocus();
//            messageVideoView.start();
//        }
//
//        messageVideoView.start();

        if (intent.getStringExtra("videoURL") == null || intent.getStringExtra("videoURL").equals("")) {
            jcVideoView.setVisibility(View.GONE);
        } else {
            jcVideoView.setVisibility(View.VISIBLE);

            Uri uri = Uri.parse(intent.getStringExtra("videoURL"));
            jcVideoView.setUp(uri.toString(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "");
            jcVideoView.requestFocus();

        }

        //messageVideoView.start();

    }

    private void init() {

        setTextViewTypeface(channelNameTextView);
        setTextViewTypeface(durationTextView);
        setTextViewTypeface(timeTextView);
        setTextViewTypeface(messageTextView);

    }

//    @Override
//    public void onBackPressed() {
//        // your code.
//
//
//        if (JCVideoPlayer.backPress()) {
//            //Log.d("VIEW_MESSAGE","BACK PRESSED");
//            return;
//        }
//        else {
////            Session session = new Session(ViewMessageActivity.this);
////            session.setIdPatient(idPatient);
//            Intent intent = new Intent(this, MessagingActivity.class);
//            //intent.putExtra("idPatient", idPatient);
//            startActivity(intent);
//        }
//        super.onBackPressed();
//
//
//
////        if (jcVideoView.currentScreen==JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN){
////            jcVideoView.currentScreen = JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL;
////        }
//
//
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
        }
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}

FullScreenMessageImage.java

package tekhinno.com.au.mygulukose;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.Picasso;

import tekhinno.com.au.mygulukose.activities.MessagingActivity;
import tekhinno.com.au.mygulukose.internet.InternetConnectivity;

public class ViewFullScreenMessageImage extends AppCompatActivity {

    FrameLayout frame1;
    TextView textView;
    private ProgressBar progressBar;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        setContentView(R.layout.activity_view_full_screen_message_image);
        frame1 = (FrameLayout) findViewById(R.id.activity_view_full_screen_message_image);
        frame1.setBackgroundColor(Color.BLACK);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        final Intent intent = getIntent();
        String title = intent.getStringExtra("imageTitle");
        getSupportActionBar().setTitle(title);
        //getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        progressBar = (ProgressBar)findViewById(R.id.image_full_screen_progressbar);
        textView = (TextView)findViewById(R.id.tv1);
        checkInternet();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent2 = new Intent(getApplicationContext(), MessagingActivity.class);
                startActivity(intent2);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void loadImage(){

        final Intent intent = getIntent();
        PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);


            if (intent.getStringExtra("imageUrl") == null || intent.getStringExtra("imageUrl").equals("")) {
                photoView.setVisibility(View.GONE);
            }else {

                Uri imageUrl = Uri.parse(intent.getStringExtra("imageUrl"));
                Picasso.with(this).load(imageUrl).into(photoView);

                progressBar.setVisibility(View.GONE);
            }

            if (intent.getStringExtra("messageContent") == null || intent.getStringExtra("messageContent").equals("")) {
                textView.setVisibility(View.GONE);
            }else {
                textView.setText(intent.getStringExtra("messageContent"));
                progressBar.setVisibility(View.GONE);
            }

        progressBar.setVisibility(View.VISIBLE);

    }

    public void checkInternet(){
        if(InternetConnectivity.isConnectedToInternet(this)==true){
            loadImage();
        }else{
            Toast.makeText(getApplicationContext(), "No Internet connection", Toast.LENGTH_LONG).show();
            progressBar.setVisibility(View.GONE);

        }

    }

}
4

0 回答 0