当前位置: 首页 > 知识库问答 >
问题:

BitmapFactory:无法解码流:java.io.FileNotFoundException:.jpg(没有这样的文件或目录)

松阳泽
2023-03-14
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: .jpg (No such file or directory)
public class PrendrePhoto extends AppCompatActivity {

private ImageView imageView;
private EditText titrImg2;
private Button take;
private String pathPic;

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

    imageView = (ImageView) findViewById(R.id.imageTaken);
    titrImg2 = findViewById(R.id.titreImg2);

    take = findViewById(R.id.take);
    take.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takePicture();
        }
    });
}

private void takePicture() {
    Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takepic.resolveActivity(getPackageManager()) != null) {
        File pic = null;
        pic = creerPhotoFile();
        if (pic != null) {
            pathPic = pic.getPath();
            System.out.println("pic créer");
            System.out.println(pathPic);
            startActivityForResult(takepic, 1);
        }else {
            System.out.println("pic null");
        }

    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent 
 data)             
  {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
           Bitmap photo = BitmapFactory.decodeFile(pathPic);
            imageView.setImageBitmap(photo);

        }
    }

    //Hna je dois crée filePhoto

}

private File creerPhotoFile() {
    ZonedDateTime now = ZonedDateTime.now();
    String d = ("" + now.getDayOfMonth() + "/" + now.getMonthValue() + 
   "/"  + now.getYear());
    String heure = "" + now.getHour() + " : " + now.getMinute();
    String titre = titrImg2.getText().toString();
    File temp = new File("temp");
    temp.mkdirs();
    System.out.println(temp.getPath());
    File image = null;
   /*   try {
        image = File.createTempFile(titre, ".jpg",temp);//Even with this 
       it didn't work 
    } catch (IOException e) {
        e.printStackTrace();
    }*/

   image = new File(titre + ".jpg");

    return image;
   }
   }
   @Entity
 public class Photo implements Serializable
 {
   @PrimaryKey(autoGenerate = true)

 private int idP;
 private String titre;
 private String path ;//this path to get to help to display this picture
 private String dateHeure ;


 public Photo(String titre, String dateHeure) {
    this.titre = titre;
    this.dateHeure = dateHeure;
 }
 }

共有1个答案

席波娃
2023-03-14

即使在完成了创建您正在使用的唯一文件路径的所有逻辑之后

字符串titre=titrimg2.getText().ToString();

这是(根据名称提示)图像标题,它来自UI。

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
}
 类似资料: