【问题标题】:BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: .jpg (No such file or directory)BitmapFactory:无法解码流:java.io.FileNotFoundException:.jpg(没有这样的文件或目录)
【发布时间】:2019-11-11 12:26:53
【问题描述】:

早上好,我正在创建拍照然后将其存储在文件中的活动。但我有这个错误:

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

Photo.java

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

【问题讨论】:

  • 看起来pathPic 的值是".jpg",并且没有同名的文件。
  • 但将其初始化为 pathPic = pic.getPath();

标签: android bitmap android-camera android-room android-file


【解决方案1】:

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

字符串滴度 = titrImg2.getText().toString();

这是(顾名思义)图像标题,来自 UI。

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

返回图片;

我认为你需要调查一下。

就在房间里保存图像而言,不建议这样做。但是如果你必须这样做,那么你可以使用 BLOB。 图像通常存储为 BLOB,而房间确实提供了这一点 数据类型。 BLOB Documentation

可以这样实现:

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
}

【讨论】:

  • 但是当我打印 titre 的值时,我得到了一个结果,它显示了我在编辑文本中输入的内容
猜你喜欢
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
相关资源
最近更新 更多