我从社交网络共享一张图像并使用我的应用程序捕获它(点击此链接),但它捕获的是纯文本而不是图像,我尝试解析为 uri 和 url,但它不起作用
void handleSendText(Intent intent) {
String sharedText2 = intent.getStringExtra(Intent.EXTRA_TEXT);
//Bn1.setText("Descargar Texto plano");
if (sharedText2 != null) { // check if is null or not
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Imgs/");
if(!folder.exists()){folder.mkdir();}// create folder
int diagonal = sharedText2.indexOf(":");
String sharedText = sharedText2.substring(diagonal + 1, sharedText2.length());
sharedText = sharedText.replace(" ", "");
if (!sharedText.startsWith("htt")) {// check if it's starts with http or not
sharedText = "https" + sharedText.substring(0, sharedText.indexOf("?"));
}
c = 1;
Bitmap bitmap = null;
Uri imageUri = Uri.parse(sharedText); // parse to uri
Time now = new Time(); // time
now.setToNow();
String nombre = "Imagen-" + now.weekDay + "-" + now.month + "-" + now.year + "-" + now.minute + "_" + now.second;// name of the image
if (imageUri != null) {
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
File file = null;
file = new File(folder.getAbsoluteFile(), nombre + ".jpg");
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
//bitmap.compress(Bitmap.CompressFormat.WEBP, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Tv1.setMovementMethod(new ScrollingMovementMethod());
Tv1.setText("Es una imagenn" + imageUri + "n" + file + "nnombre:n" + nombre); // show the name of the variables
if (bitmap == null) {
Tv1.setText(sharedText2 + "nnsharedtextn" + sharedText);
} else {
Tv1.setText("SI quedon" + sharedText);
}
}
}
}
EXTRA_TEXT
应该是纯文本。 另一方面,EXTRA_STREAM
应该是content:
Uri
(尽管通常你会得到一个file:
Uri
)。据推测,您寻求的信息将在EXTRA_STREAM
,而不是EXTRA_TEXT
。