有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何处理从相机保存图像时的错误?

我想做一个带照相机的应用。点击图片后,您可以将图片储存在手机中。我看过一些教程,并相应地编写了代码(我是Android studio的新手)。 然而,当其他人没有得到语法错误时,我却得到了语法错误。 我有两项活动。下面是我的主要活动。爪哇:

package com.none.www.dumpcam;

import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.Bitmap;
import 安卓.net.Uri;
import 安卓.os.Environment;
import 安卓.provider.MediaStore;
import 安卓.support.v4.content.FileProvider;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ImageView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    String currentImagePath = null;

    private ImageView mimageView;
    private static final int IMAGE_REQUEST = 1;

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

    public void captureImage(View view){

        Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(imageTakeIntent.resolveActivity(getPackageManager())!=null){
            File imageFile = null;
            try {
                imageFile = getImageFile();
            } catch (IOException e){
                e.printStackTrace();
            }
        if(imageFile!=null){

            Uri imageUri = FileProvider.getUriForFile(context:this, authority:"com.example.安卓.fileprovider", imageFile); //one error is shown here
            imageTakeIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(imageTakeIntent, IMAGE_REQUEST);
            }

        }

    }

    public void displayImage(View view){
        Intent intent = new Intent(this,DisplayImage.class);
        intent.putExtra(name: "image_path", currentImagePath);
        startActivity(intent);
    }

        private file getImageFile() throws IOException{
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageName = "jpg_"+timeStamp+"_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, suffix ".jpg", storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        return imageFile;
    }

}

下面是我的显示图像。爪哇:

package com.none.www.dumpcam;

import 安卓.graphics.Bitmap;
import 安卓.graphics.BitmapFactory;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.widget.ImageView;

public class DisplayImage extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_image);
        imageView = findViewById(R.id.mimageView);
        Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra( name: "image_path"));  //One error is shown here. 
        imageView.setImageBitmap(bitmap);
    }
}

就像我说的,我正在编写教程中的代码。代码运行良好。 我发现以下语法错误:

com/none/www/dumpcam/DisplayImage.java  
error: ')' expected 
error: ';' expected 
error: illegal start of expression  
error: ';' expected 
com/none/www/dumpcam/MainActivity.java  
error: ')' expected 
error: not a statement  
error: ';' expected 
error: not a statement  
error: ';' expected 
error: not a statement  
error: ';' expected 
error: not a statement  
error: not a statement  
error: ')' expected 
error: ';' expected

共 (0) 个答案