如何在Android中创建照片应用程序?

时间:2014-09-25 00:14:51

标签: java android xml

我的程序不再运行了。它只是强制关闭。之前我用它从互联网上下载并放入我的drawable文件夹中的两张照片。但我删除了虽然并添加了一些我从旅行中拍摄的照片。我更新了xml和java代码,但现在它不会打开。这是我的代码。

package com.example.photoalbum;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class DisplayPhotos extends Activity
{
ImageView iv1, iv2, iv3, iv4;
private Integer [] imageArray = {R.drawable.nepal1,
R.drawable.nepal2,R.drawable.nepal3, R.drawable.nepal4};

int imageCount = 0;

//******************onCreate()*******************************   
@Override
public void onCreate(Bundle b) 
{
    super.onCreate(b);
    setContentView(R.layout.activity_display_photos);
    Button btn1 = (Button) findViewById(R.id.previous_btn);
    Button btn2 = (Button) findViewById(R.id.next_btn);
    iv1 = (ImageView) findViewById(R.id.nepal1);
    iv2 = (ImageView) findViewById(R.id.nepal2);
    iv3 = (ImageView) findViewById(R.id.nepal3);
    iv4 = (ImageView) findViewById(R.id.nepal4);
    ImageView imgs [] = new ImageView[4];
    imgs[0] = iv1;
    imgs[1] = iv2;
    imgs[2] = iv3;
    imgs[3] = iv4;

    showImage();
}
//*************************showImage()********************* 
public void showImage()
{
    iv1.setImageResource(imageArray[imageCount]);
}


//***************************doit************************** 
    public void doit(View v)
{
    switch (v.getId())
    {
        case (R.id.previous_btn):
            imageCount--;
            if (imageCount == -1)
                imageCount = 3;
            showImage();
            break;

        case (R.id.next_btn):
            imageCount++;
            if (imageCount == 4)
                imageCount = 0;
            showImage();
            break;
    }
}
}

我的xml代码

<FrameLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/nepal1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/nepal2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageView
    android:id="@+id/nepal3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageView
    android:id="@+id/nepal4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <Button
        android:id="@+id/previous_btn"
        android:layout_width="163dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:onClick="doit"
        android:text="@string/previous" />

    <Button
        android:id="@+id/next_btn"
        android:layout_width="163dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:onClick="doit"
        android:text="@string/next" />

 </FrameLayout>

我的logcat

09-24 20:09:42.243: D/dalvikvm(1750): GC_FOR_ALLOC freed 61K, 5% free 2941K/3084K, paused 44ms, total 47ms
09-24 20:09:42.243: I/dalvikvm-heap(1750): Forcing collection of SoftReferences for 55987216-byte allocation
09-24 20:09:42.303: D/dalvikvm(1750): GC_BEFORE_OOM freed 12K, 6% free 2929K/3084K, paused 62ms, total 62ms
09-24 20:09:42.303: E/dalvikvm-heap(1750): Out of memory on a 55987216-byte allocation.
09-24 20:09:42.313: I/dalvikvm(1750): "main" prio=5 tid=1 RUNNABLE
09-24 20:09:42.313: I/dalvikvm(1750):   | group="main" sCount=0 dsCount=0 obj=0xb3a76ca8 self=0xb75e2398
09-24 20:09:42.313: I/dalvikvm(1750):   | sysTid=1750 nice=0 sched=0/0 cgrp=apps handle=-1225387692
09-24 20:09:42.323: I/dalvikvm(1750):   | state=R schedstat=( 570000000 1280000000 122 ) utm=31 stm=25 core=0
09-24 20:09:42.323: I/dalvikvm(1750):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-24 20:09:42.323: I/dalvikvm(1750):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.content.res.Resources.loadDrawable(Resources.java:2110)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.content.res.Resources.getDrawable(Resources.java:700)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.widget.ImageView.resolveUri(ImageView.java:638)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.widget.ImageView.setImageResource(ImageView.java:367)
09-24 20:09:42.333: I/dalvikvm(1750):   at com.example.photoalbum.DisplayPhotos.showImage(DisplayPhotos.java:50)
09-24 20:09:42.333: I/dalvikvm(1750):   at com.example.photoalbum.DisplayPhotos.onCreate(DisplayPhotos.java:45)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.app.Activity.performCreate(Activity.java:5231)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-24 20:09:42.333: I/dalvikvm(1750):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-24 20:09:42.343: I/dalvikvm(1750):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-24 20:09:42.343: I/dalvikvm(1750):   at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 20:09:42.343: I/dalvikvm(1750):   at android.os.Looper.loop(Looper.java:136)
09-24 20:09:42.343: I/dalvikvm(1750):   at android.app.ActivityThread.main(ActivityThread.java:5017)
09-24 20:09:42.343: I/dalvikvm(1750):   at java.lang.reflect.Method.invokeNative(Native Method)
09-24 20:09:42.343: I/dalvikvm(1750):   at java.lang.reflect.Method.invoke(Method.java:515)
09-24 20:09:42.343: I/dalvikvm(1750):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-24 20:09:42.343: I/dalvikvm(1750):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-24 20:09:42.343: I/dalvikvm(1750):   at dalvik.system.NativeStart.main(Native Method)
09-24 20:09:42.343: D/skia(1750): --- decoder->decode returned false
09-24 20:09:42.343: D/AndroidRuntime(1750): Shutting down VM
09-24 20:09:42.343: W/dalvikvm(1750): threadid=1: thread exiting with uncaught exception (group=0xb3a75ba8)
09-24 20:09:42.383: E/AndroidRuntime(1750): FATAL EXCEPTION: main
09-24 20:09:42.383: E/AndroidRuntime(1750): Process: com.example.photoalbum, PID: 1750
09-24 20:09:42.383: E/AndroidRuntime(1750): java.lang.OutOfMemoryError
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.content.res.Resources.loadDrawable(Resources.java:2110)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.content.res.Resources.getDrawable(Resources.java:700)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.widget.ImageView.resolveUri(ImageView.java:638)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.widget.ImageView.setImageResource(ImageView.java:367)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at com.example.photoalbum.DisplayPhotos.showImage(DisplayPhotos.java:50)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at com.example.photoalbum.DisplayPhotos.onCreate(DisplayPhotos.java:45)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.Activity.performCreate(Activity.java:5231)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.os.Looper.loop(Looper.java:136)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at java.lang.reflect.Method.invoke(Method.java:515)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-24 20:09:42.383: E/AndroidRuntime(1750):     at dalvik.system.NativeStart.main(Native Method)
09-24 20:09:51.043: I/Process(1750): Sending signal. PID: 1750 SIG: 9

1 个答案:

答案 0 :(得分:0)

可能是您的新图片太大,您的应用程序正在尝试分配55987216字节及其导致内存分配异常。看一下能够很好地处理图像/照片的一些android libraries like picasso

有关详细信息和其他选项take a look at this SO question