
大家好,我这个菜鸟又来了。 我之前得到一个任务, 就是在 android studio 里面加载一个.tif 文件格式的图像, 然后进行一个图像转换( python code 处理 image tranformation ,比如 gaor filter/transformation )的方法。 我的问题: android studio 手机先要从真实手机里加载.tif 的图像 1.首先我在我的真实的手机下存储了.tif 的10个图像, 2.然后我现在我的 androidstudio 里设置了一个从手机里选择 galley 选取一张.tif 的图片, 3.然后用 imageview 展示在 app 里。
我有个疑问,.tif 格式的图像真的能正常在手机屏幕上显示出来吗,因为我无法看到。tif 的图像显示在屏幕上?
我的代码: M ainActivity.java import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.Manifest; import android.annotation.SuppressLint; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;
import com.chaquo.python.PyObject; import com.chaquo.python.Python; import com.chaquo.python.android.AndroidPlatform;
import org.beyka.tiffbitmapfactory.TiffBitmapFactory;
import java.io.FileNotFoundException; import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView textView8; //private static final int Read_Permission= 101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button gallery = findViewById(R.id.gallery); gallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent,3); } }); /*if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission); } */
textView8 = (TextView) findViewById(R.id.textView8); if(!Python.isStarted()){ Python.start(new AndroidPlatform(this)); } Python py = Python.getInstance(); PyObject pyobj = py.getModule("hello"); // give python script name // now call this function PyObject obj = pyobj.callAttr("main"); // now set return text to textview textView8.setText(obj.toString()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK && data != null){ try { // from the Beyla original code ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(data.getData(), "r"); Bitmap bmp = TiffBitmapFactory.decodeFileDescriptor(parcelFileDescriptor.getFd()); ImageView imageView = findViewById(R.id.imageView); imageView.setImageBitmap(bmp); } catch (FileNotFoundException e) { e.printStackTrace(); } // Uri selectedImage = data.getData(); // ImageView imageView = findViewById(R.id.imageView); // imageView.setImageURI(selectedImage); } } }
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:cOntext=".MainActivity" tools:ignore="MissingClass">
<ImageView android:id="@+id/imageView" android:layout_width="250dp" android:layout_height="250dp" android:layout_centerInParent="true" tools:layout_editor_absoluteX="80dp" tools:layout_editor_absoluteY="194dp" tools:ignore="MissingConstraints" /> <Button android:id="@+id/gallery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Pick Image" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="125dp" tools:layout_editor_absoluteY="556dp" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="33dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.436" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pick" app:layout_constraintVertical_bias="0.2" tools:ignore="MissingConstraints" /> </androidx.constraintlayout.widget.ConstraintLayout>
1 momosolaris OP |
2 kop1989smurf 2022-11-15 09:02:20 +08:00 .tif 文件不能在原生图片选择器中预览(当然你可以自己实现一个,就可以预览了) 除了以上,这个流程没什么问题。 |
3 momosolaris OP @kop1989smurf 谢谢你的回答,我也查了一下,确实不能在 app 里呈现。我只能把它转成 png 的形式。 |