繁体   English   中英

为什么我的方法重新定义null值?

[英]why my method retruniing null value?

我已将变量目录声明为全局目录,并在下面的returnData方法螺母中使用该变量返回了null值。

public void SaveImage(String FileName, Bitmap mBitmap) {

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();

        File directory = new File(root + File.separator + "HMS_BARCODE");
        directory.mkdirs();
        //create a file to write bitmap data
        File f = new File(directory, FileName + ".png");
        Log.e("dir", "" + directory);
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("IOException", "IOException");
        }

        //Convert bitmap to byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bytearray = bos.toByteArray();

        //Write bytes in file
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            fos.write(bytearray);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Exception", "" + e);
        }

    } else {

        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        directory = new File(root + File.separator + "HMS_BARCODE");
        if (!directory.exists()) {
            directory.mkdirs();
        }

        File f = new File(directory, FileName + ".png");
        Log.e("dir1", "" + directory);



        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("IOException", "IOException");
        }
        Log.e("dir1", "" + directory);

        //Convert bitmap to byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bytearray = bos.toByteArray();

        //Write bytes in file
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            fos.write(bytearray);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Exception", "" + e);
        }

    }


}

// this is method returning null value I want want that directory
// value to pass to another class
public File returnData() {
    Log.e("Exception", "" + directory);
    return directory;
}

请正确格式化您的问题,很难阅读。 从我可以看到,您的目录变量不是全局变量,对于saveImage方法是本地变量。

如果要从同一类实例的不同方法访问目录变量,则需要将其声明为类变量。 例如:

public class MyClass {

    private File directory;

    public void saveImage(...) {....}

    public File returnData(...) {...}
}

您必须在returnData方法中调用SaveImage方法:

//this is method returning null value i     want want that directory value to     pass to another class
public File returnData(){
    SaveImage(.../*the parameters*/);
    Log.e("Exception", "" + directory);
    return directory;
}

暂无
暂无

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

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