0

我是安卓新手。我正在尝试进行某种图像处理。但我收到此消息“此应用程序 --- 已意外停止。请重试。”。请告诉我我犯了什么错误

package com.imagep.amit;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class ImagepActivity extends Activity {
    /** Called when the activity is first created. */

    Bitmap myBitmap;
    ImageView myImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String imageFileName= "/sdcard/test_vga.jpg";
        File imageFile= new File(imageFileName);
        if (imageFile.exists()) {
            // Load the image from file
            myBitmap= BitmapFactory.decodeFile(imageFileName);
            // Display the image in the image viewer
            myImageView= (ImageView)findViewById(R.id.di);
            if (myImageView!= null) {
                myImageView.setImageBitmap(myBitmap);
            }
        }
        this.processImage();
    }

    private void processImage() {
        brighten(50);
        try {
            String outputPath= "/test_vga_output.jpg";
            int quality = 75;
            FileOutputStream fileOutStr= new FileOutputStream(outputPath);
            BufferedOutputStream bufOutStr= new BufferedOutputStream(fileOutStr);
            myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
            bufOutStr.flush();
            bufOutStr.close();
        } catch (FileNotFoundException exception) {
            Log.e("debug_log", exception.toString());
        } catch (IOException exception) {
            Log.e("debug_log", exception.toString());
        }
        myImageView.setImageBitmap(myBitmap);
    }

    private void brighten(int i) {
        int width = myBitmap.getWidth();
        int height = myBitmap.getHeight();
        int[] pix = new int[width * height];
        myBitmap.getPixels(pix, 0, width, 0, 0, width, height);
        // Apply pixel-by-pixel change
        int index = 0;
        for(int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int r = (pix[index] >> 16) & 0xff;
                int g = (pix[index] >> 8) & 0xff;
                int b = pix[index] & 0xff;
                r = 0;
                g = 0;
                b = 0;
                pix[index++] = 0xff000000| (r << 16) | (g << 8) | b;
            } // x 
        } // y
        // TODO Auto-generated method stub
    }
}
}
4

1 回答 1

2

使用adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图来检查 LogCat 并查看与您的错误相关的堆栈跟踪。

此外,永远不要使用硬连线路径,尤其是因为/sdcard它是错误的——用于Environment.getExternalStorageDirectory()获取外部存储的根目录。

最后,您的实际问题可能来自:

String outputPath= "/test_vga_output.jpg";

这是一个无效的路径。我不知道你认为你在哪里写,但你不能在那里写。但是,您可能还有其他问题,堆栈跟踪将帮助您识别它们。

于 2012-04-16T14:41:33.663 回答