简体   繁体   English

android应用程序做力关闭

[英]android application doing force close

I am new to android. 我是android的新手。 I am trying to do some kind of image processing. 我正在尝试做某种图像处理。 But I am getting this message "This application --- has stopped unexpectedly. Please try again.". 但是我收到这条消息“这个应用程序---意外停止了。请再试一次。” Kindly tell me what mistake I am making 请告诉我我犯了什么错误

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
    }
}
}

Use adb logcat , DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your error. 在Eclipse中使用adb logcat ,DDMS或DDMS透视图来检查LogCat并查看与错误关联的堆栈跟踪。

In addition, never hardwire paths , particularly since /sdcard is wrong -- use Environment.getExternalStorageDirectory() to get the root of external storage. 此外, 永远不要硬连线路径 ,特别是因为/sdcard错误 - 使用Environment.getExternalStorageDirectory()来获取外部存储的根。

Finally, your actual problem probably comes from: 最后,您的实际问题可能来自:

String outputPath= "/test_vga_output.jpg";

which is an invalid path. 这是一条无效的路径。 I do not know where you think you are writing, but you cannot write there. 我不知道你在哪里写作,但你不能写在那里。 However, you may have additional problems beyond that, and the stack trace will help you identify them. 但是,除此之外,您可能还有其他问题,堆栈跟踪将帮助您识别它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM